CMD Search a directory to Find a string inside a file
Asked Answered
J

4

9

I need to find the files in a directory that have specific strings, using Windows CMD prompt.

E.g., I need to find the files that have a string like this:

<h1>Select an Item</h1>
Jujutsu answered 23/10, 2014 at 21:17 Comment(0)
T
14

"findstr" iswhat you are looking for.

findstr /I "<h1>Select\ an\ Item</h1>" *.*

findstr is the command, /I is a flag to match the string case insensitive. "<h1>Select\ an\ Item</h1>" is your string (note the escaped spaces!) and *.* means "in all files in this directory".

The basic syntax is findstr "seachString" filename.ext. You may replace filename.ext with *.ext or *.* to filter cretin file types or look in all files. This will look only in the current directory and not recursively.

More information about the command findstr documentation

Tannenwald answered 24/10, 2014 at 0:19 Comment(1)
just add /S if you want results for subfolders as wellKelt
B
4

The command you require is fundamentally findstr.

type

findstr /?

at the prompt for directions.

The command that may work for you is

findstr /m /g:"a file containing your string or strings" *

or

findstr /m /L /c:"<h1>Select an Item</h1>" *

Where some experimentation with the contents of the "quoted string" may be required, especially wrt characters line <>() and others with a particular meaning to cmd.exe.

Bathsheb answered 24/10, 2014 at 0:23 Comment(0)
H
2
  1. Open up a Windows console (or CMD).
  2. Go to the folder, from where you want to start the search.
  3. Let's say you search for a text (e. g. "abc") in a folder, specifically in all the Word files (e. g. ".doc" and "docx"). Then the command should look like this
findstr /s /i /m /c:"abc" *.docx *.doc

You will get a list with all the Word documents, that contain the text, including the the subfolders.

Hypogeous answered 26/4, 2023 at 12:55 Comment(0)
J
1

Find some string inside all text files in current directory:

cls & for %i in (*.txt) do find /i "search text" < "%i" && (echo : %i & echo -)

Tested in Win 10

Jeremy answered 18/5, 2022 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.