Windows – Trying to take the line from a logfile, remove some text, and put it into a new text file in a batch script

batchscriptwindows 10

I am trying to take the last line from a logfile and remove some text from the beginning and end of it and put it into a new text file.
I have minimal batch experience, any help is appreciated.

last line:

Opening track for playback: "D:\Music\musicfile.m4a"

Path to the logfile:

%USERPROFILE%\Documents\file.log

What I want the last line to look like when it is done:

musicfile

Best Answer

PowerShell:

Verbose:

$LogFile = "$env:%USERPROFILE%\Documents\file.log"
(Get-Content -Path $LogFile | Select -Last 1) -match '.+\\([\w\s]+)\.\w{3}"$' | out-null
$matches[1]

At the console:

(gc "$HOME\Documents\file.log")[-1] -match '.+\\([\w\s]+)\.\w{3}"$'
$matches[1]

Alternate string manipulation:

$Line = (gc "$HOME\Documents\file.log")[-1]
$Line.split('"')[1].split('\')[-1].split('.')[0]

or even a one-liner:

(gc "$HOME\Documents\file.log")[-1].split('"')[1].split('\')[-1].split('.')[0]

Get-Content

Select-Object

About Regular Expressions

Related Question