0

I am using findstr to search an string in some file and redirecting the search result to another txt file but need to omit the searched file name added in results.

My batch file has :- findstr /s "TEST" subfolder/test.dat > output.txt

and the result of output.txt with the filename test.dat(which I need to remove):-

subfolder/test.dat:2014-04-15;TEST TECHNOLOGY LTD

Same kind of question has been asked here. But in my case I am not using any wildcards. Please help.

Community
  • 1
  • 1
Yash
  • 173
  • 2
  • 13
  • I can't reproduce. Can you reproduce it on a different file or when running it in a command prompt console? – wOxxOm Sep 08 '15 at 18:02
  • @wOxxOm I tried both ways but same results. – Yash Sep 08 '15 at 18:06
  • 1
    Well, AFAIK it just can't be unless you have `/s` switch to look for file recursively. – wOxxOm Sep 08 '15 at 18:07
  • @wOxxOm yes, I am using `/s` but I need it as I need to search in sub directories as well. Any suggestions for alternative way. Thanks! – Yash Sep 08 '15 at 18:10

2 Answers2

1
  1. Parse the output of recursive dir:

    >output.txt (
        for /f "eol=* delims=" %%a in ('dir /s /b "subfolder\test.dat"') do (
            findstr /c:"TEST" "%%a"
        )
    )
    
    • eol=* is used to correctly parse folders with ; which is treated as comment by for /f
    • delims= (empty) means that linebreak is used to delimit the output, so the line is taken entirely
    • /c: is used to indicate literal search string so that it may contain spaces
  2. Alternatively you can strip the file names from the recursive findstr's output:

    >output.txt (
        for /f "delims=: tokens=1*" %%a in (
            'findstr /s /c:"TEST" "subfolder\test.dat"'
        ) do echo.%%b
    )
    

    In case you want to specify a drive qualifier use tokens=2*.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
0

One more way is to pipe with TYPE

If there's only one file:

type "subfolder/test.dat" |Findstr "TEST" > output.txt

The S switch is for searching in subfolders so if the files are more:

for /r "subfolder" %%a in (*.dat) do type %%~fa|Findstr "TEST" >> output.txt
npocmaka
  • 55,367
  • 18
  • 148
  • 187