Question

Looking for the command line (or batch) that will do the following,

I have 3 levels of directories and need to search for a specific string contained in text files, searching all files, 1 level subdirectory down.

So I know this will recursively search for the string

findstr /S "STRING" *

But I don't want the search to go father than only 1 subdirectory level down.

Seems like a simple thing but I could not find anything solid. The closest thing I could find was something like this thread..

windows batch to recursively scan files in 1 level subfolders

However, before going batch is there a simpler (command line) way to this rather, apparently simple task?

Thanks for your time!

Was it helpful?

Solution

This should do it from the command line and write file.log into the current folder

for /d %a in (*) do findstr "STRING" "%a\*">>file.log

OTHER TIPS

Child Directory Search Only (No Parents or Grandchildren)

Here is a one line command to enumerate the current working directory children directories dir /ad/b and search each directory for files containing the search string. Results are saved out to a text file.

for /f "delims=" %A in ('dir /ad/b') do @findstr "STRING" "%~A\*" >> results.txt

Note: That as currently configured, a result line will be output for each matching line in a file. Meaning that multiple find results may be output for a single file.

Example of results.txt

ChildFolderA\File1.txt:line with STRING
ChildFolderA\File1.txt:another line with STRING in file1
ChildFolderA\File2.txt:another file and line with STRING
ChildFolderB\File5.txt:another folder with a file containing the search STRING
@echo off
setlocal enableextensions
(@for /d %%a in (*) do @for %%b in ("%%~fa\*") do @echo(%%~fb)|findstr /f:/ /c:"string" > file.txt

This will enumerate all the files one level down and send this list of files to a findstr command. findstr will search the string into the indicated list and send output to final file

To use from command line, replace the double percent signs %% with single ones %

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top