Stulander
Stulander

Reputation: 13

How can I search within many files for a specific string and get returned the names of all files containing the string?

I have a folder with many *.inf files. I want to search the contents of each file for the string prolific. If one of these files contains prolific then it should return the name of that file so I can do other operations on it like deleting or renaming it.

I'm new to this. This is my initial effort, but it doesn't work and I'm not sure where to go from here. (I'm sure I could have done this a few years ago but I've forgotten everything.)

@echo OFF

For %%a (%windir%\inf\oem*.inf) do (
    for /f "skip=2 tokens=*" %%b in ('find "prolific" "%%a"') do (
        Echo %%~nx# >> "%~dp0\output.txt"
    )
)

Pause

Upvotes: 1

Views: 160

Answers (1)

Mofi
Mofi

Reputation: 49084

This can be done with a single command line using FINDSTR:

%SystemRoot%\System32\findstr.exe /I /M "prolific" %SystemRoot%\inf\oem*.inf >>"%~dp0output.txt"

Open a command prompt window and run findstr /? for help on this standard console application of Windows.

/I ... search case-insensitive
/M ... output just the file names containing the searched string.

Note: The drive and folder path of the batch file referenced with %~dp0 ends always with a backslash. So don't add an additional backslash.

Upvotes: 2

Related Questions