user841311
user841311

Reputation: 597

findstr or grep output to a file

Im in Windows Server 2003 and using below commands to fetch string matching lines in files created today in a specific directory.

forfiles -p D:\ -m *.1  -d +0 -c "cmd /c findstr /i /c:\"Hey Hi\" @FILE" >> txt5.txt
or
forfiles -p D:\ -m *.1  -d +0 -c "cmd /c grep \"Hey Hi\" @FILE" >> txt5.txt

Geeting error 'FINDSTR: Write error' and 'grep write error bad file descriptor' respectively for both commands. So basically the commands work i.e. display the output in screen but unable to redirect the output to a file.

I did not find suitable solution though users reported this same error for different scenarios. Any help is appreciated. Thanks in advance!

Upvotes: 1

Views: 3316

Answers (1)

Ken White
Ken White

Reputation: 125708

You should be able to remove the cmd /c; I don't see any reason you'd need a new copy of the command shell open for the findstr call.

This works for me correctly at the command prompt:

forfiles -m t*.xml -d +0 -c "findstr /i "Item" @file" >> out.txt. 

It produces an out.txt file that contains the proper content matching the search criteria.

Upvotes: 2

Related Questions