0

I have my CLI query set to:

c:\>findstr /c:"TOTAL" Invoice.txt 
TOTAL 80:00

However I only want the search query to return everything after the query:

80.00

Also if I use wild in the filename it returns the whole file name then the line. I want it again to only return everything after the string and not the filename as I want to pipe the results into a text file.

c:\>findstr /c:"TOTAL" *.txt
Invoice - Copy (2).txt:TOTAL 120.00
Invoice - Copy (3).txt:TOTAL 110.00
Invoice - Copy (4).txt:TOTAL 100.00
Invoice - Copy.txt:TOTAL 90.00
Invoice.txt:TOTAL 80.00

Ideally I run my command and just get the following

120.00
110.00
100.00
90.00
80.00

Ideas on how to do this? Powershell or CMD is fine. At the moment I am going to put it all into a batch script but a ps script would work.

Thanks!

Peter Dee
  • 27
  • 1
  • 4

4 Answers4

0

To get the output of a command, use a for /f loop:

for /f "tokens=2" %%a in ('findstr /c:"TOTAL" *.txt') do echo %%a

Given, your data is always as your example (line is exactly TOTAL xxx.xx and there is no other TOTAL in the files (may want to use findstr /b))

(batch-syntax. To use it directly on command line, replace every %%a with %a)

EDIT a bit more complicated with spaces in the filenames. Do it in two steps: first get the pure data splitting by : ("tokens=2 delims=:" %%a) and in a second for split by space (standard delimiter) ("tokens=2" %%b) :

for /f "tokens=2 delims=:" %%a in ('findstr "TOTAL" *.txt') do (
  for /f "tokens=2" %%b in ("%%a") do echo %%b
)

(a bit more code, but better than messing with the original data (renaming files))

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Was close but not quite there, I had to first remove the white spaces https://stackoverflow.com/questions/11270453/how-to-remove-spaces-from-file-names-in-bulk in the filenames then your for /f loop worked. – Peter Dee Jun 02 '17 at 07:44
  • sorry, I missed the spaces in the filenames. See my edit please. – Stephan Jun 02 '17 at 08:15
0

A for /F loop can capture the output of the findstr command line. After having stored each line in an environment variable, sub-string replacement can be applied in a way that everything up to and including : + TOTAL + SPACE becomes removed:

setlocal EnableDelayedExpansion
for /F "delims=" %%L in ('findstr /C:"TOTAL" "*.txt"') do (
    set "LINE=%%L"
    echo(!LINE:*:TOTAL =!
)
endlocal
aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

Just my 2 cents ;-) . The first for can be a simple one just to iterate the file names.

@Echo off
( For %%A in (*.txt
  ) Do For /f "tokens=2" %%B in ('findstr /B /C:"TOTAL" "%%A"'
  ) Do Echo:%%B
) >All-Totals.txt
0

Also easy in PowerShell.

PS C:\src\t> Get-Content .\Invoice.txt | Where-Object { $_ -match '.*TOTAL (.*)' } | % { $matches[1] }
120.00
110.00
100.00
90.00
80.00

PS C:\src\t> cat .\Invoice.txt | where { $_ -match '.*TOTAL (.*)' } | % { $matches[1] }
120.00
110.00
100.00
90.00
80.00
lit
  • 14,456
  • 10
  • 65
  • 119