Link to home
Start Free TrialLog in
Avatar of Dipesh Patel
Dipesh Patel

asked on

Need help with findstr command to find string and store output

Hi Experts,
 We have windows 2012 server which has three drives. We have lots of files and folders. Our requirement to find specific hard coded  Texts (server1,server2,servers,server4)  to all files and folders. I heard windows has findstr command to find string to any files and folders.

I need to find few keywards and need to scan all drives. I would like to store the output in any text file.

Can you please help me to write script to find string to any files and folders and store output in text file?

Thank you.
Avatar of Qlemo
Qlemo
Flag of Germany image

It is usually a bad idea to search the complete system drive, but here you go:
@echo off
set text=server1,server2,server3,server4
set report=C:\Temp\result.txt
set drives=C: D: E:

copy nul %report% >nul:
for %%D in (%drive%) do findstr /Lis /c:"%text%" %%d:\*  | find /v "%text%" >> %report%

Open in new window

This searches for exact matches of the complete string.
If you need to search for multiple independent keywords:
@echo off
set text=server1 server2 server3 server4
set report=C:\Temp\result.txt
set drives=C: D: E:

copy nul %report% >nul:
for %%D in (%drive%) do findstr /Lis "%text%" %%d:\*  | find /v "%text%" >> %report%

Open in new window

Avatar of Bill Prew
Bill Prew

Clemo,

I think you have doubled up on the colon after the drive letters:

set drives=C: D: E:
for %%D in (%drive%) do findstr /Lis /c:"%text%" %%d:\*

Also, why the use of FIND on the FINDSTR output, seems like FINDSTR should be enough?

Dipesh,

You might think about limiting to specific extensions, as is you are searching all file types including binary files, which if matches are found could add a lot of "noise" to the log file that make them hard to read.


»bp
You are correct, I've messed up my code, this is not what I wanted to post :<
@echo off
set text=server1,server2,server3,server4
set report=C:\Temp\result.txt
set drives=C D E

copy nul %report% >nul:
for %%D in (%drive%) do findstr /Lis /c:"%text%" %%d:\*  | find /v "%report%" >> %report%

Open in new window

The find prevents the result file to be repeated in itself.
Avatar of Dipesh Patel

ASKER

Thank you so much for your help. Sorry i could not reply you on time. You are right that it's not a good way to scan whole drive.

Is it possible to look only some extension like web.config ?

Thank You.
That's easy:
@echo off
set text=server1,server2,server3,server4
set report=C:\Temp\result.txt
set drives=C D E
set filemask=web.config

copy nul %report% >nul:
for %%D in (%drives%) do findstr /Lis /c:"%text%" "%%d:\%filemask%"  | find /v "%report%" >> %report%

Open in new window

You can set the file mask to something less specific like *.txt if required.
Hi,
 Did you test it? It’s not working for me. I have created couples of config files and add some test to test it. I have put in C folder.

I ran script. It was so quick to finish it. It created the result.txt file but it’s just empty.

I created bat file and tried to execute by command line. Did i miss any steps? Can you please validate with your laptop?

I am using Windows 10 for testing.

Thank You.
I've found another typo and adjusted the code snippet above - %drive% should have been %drives%.
Hi Qlemo,
 I am so sorry to say but it's still not working for me. It's finish in sec and no result in result.txt. I used only C drive for testing.

@echo off
set text=server1,server2,server3,server4
set report=C:\Temp\result.txt
set drives=C
set filemask=web.config
copy nul %report% >nul:
for %%D in (%drives%) do findstr /Lis /c:"%text%" "%%d:\%filemask%"  | find /v "%report%" >> %report%

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll stop posting now, and contemplate about life, error rates and the number 42 :/.
Hi Qulemo,
 We are human being. Thank you for the script. It'a all good now.

Thank you Bill for your help too.
Welcome.