Skip to main content

Posts

Showing posts from March, 2010

Cmd.exe - Using For Loops To Simulate "grep -r */Filename" on Windows

FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used. command Specifies the command to carry out for each file. command-parameters Specifies parameters or switches for the specified command. To perform a grep operation on all files with a certain name in subdirectories of the current directory, although this will try to grep Folder\Filename.ext even if Filename.ext doesn't exist in the folder. for /d %I in (*.*) do grep grepString %~sI\Filename.ext This is slower, but it won't pass files that don't exist to grep, and will recursively search all subdirectories under the current directory. The first example only goes one level deep. for /f "usebackq delims=;" %I in (`dir /s/b Filename.ext`) do grep grepString "%I" The "delims=;" (you can use almost anything for the ;) is required if there are spaces in t