Searching for Files
In Command Prompt, the command you use depends on what you are trying to find.
In short, use dir /s /b to search by file name, and findstr /s /i /m to search by file content.
dir /s /b "C:\work\*.pdf"
cd /d "C:\work"
findstr /s /i /m "invoice" *.txt
Choosing a Search Method
| What you want to find | Command to use |
|---|---|
| Search by file name or extension | dir /s /b |
| Search for several patterns at once | where /R |
| Search by text inside files | findstr /s /i /m |
| Narrow down by modified date | forfiles /D |
| Process the files you found in a batch | for /r |
Searching by File Name (dir /s /b)
Adding /s (include subfolders) and /b (show paths only) to the dir command lists the full path of every file that matches.
dir /s /b "C:\work\*.pdf"
If you only know part of the file name, put * before and after it.
dir /s /b "C:\work\*report*"
If no file matches, dir displays “File Not Found” and usually returns exit code (ERRORLEVEL) 1. You can use this in a batch file to tell whether anything was found, but if you pipe the output, as in dir ... | findstr, ERRORLEVEL comes from the findstr that ran last.
Finding Only Folders
Adding /ad limits the search to folders (directories).
dir /s /b /ad "C:\work\*backup*"
Finding Only Files
Adding /a-d excludes folders and limits the search to files.
dir /s /b /a-d "C:\work\*report*"
A wildcard with a three-character extension (such as *.htm) can also match files with longer extensions (such as .html) because of the effect of 8.3 short names. If you need an exact match, filter the results with findstr (example: dir /s /b "C:\work\*.htm" | findstr /i "\.htm$"). See
for details.
Narrowing Down the Results
Piping the result of dir /s /b into findstr keeps only the paths that contain a specific string.
dir /s /b "C:\work\*.pdf" | findstr /i "2026"
Note that this method also checks folder names. If you want to filter by file name only, specifying a wildcard on the dir side is more reliable.
Searching with where /R
where /R searches recursively under the specified folder and displays the full path of each file it finds.
where /R "C:\work" *.pdf
The result is almost the same as dir /s /b. A distinguishing feature of where is that you can specify multiple patterns separated by spaces.
where /R "C:\work" *.pdf *.docx
In PowerShell, where is an alias for Where-Object, so include the extension and run it as where.exe /R "C:\work" *.pdf.
For more about where, see
Searching File Content (findstr)
To search by “the text written inside a file” rather than the file name, use findstr.
cd /d "C:\work"
findstr /s /i /m "invoice" *.txt
| Option | Meaning |
|---|---|
/s | Search the current folder (or the folder given as a path) and its subfolders |
/i | Ignore case |
/m | Show only file names, not the matching lines |
/n | Also show the line number of each matching line |
Without /m, the content of each matching line is displayed as well.

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
findstr works correctly only on text files (.txt, .log, .csv, .bat, source code, and so on). Word (.docx), Excel (.xlsx), and PDF files use compressed or proprietary formats internally, so the text inside them is not found correctly.
findstr cannot search UTF-16 files. Even in UTF-8 files, non-English text may not be found unless it matches the console code page (932 by default). If text in a non-English language is not found, check the encoding of the file. Encodings are explained in
For more about findstr, see
Narrowing Down by Modified Date (forfiles)
forfiles is handy when you want to filter by modified date, such as “log files last modified 30 or more days ago”.
forfiles /P "C:\work" /S /M *.log /D -30 /C "cmd /c echo @path"
/D -30 means “modified 30 days ago or earlier”. @path is the full path of the file that was found, output in quotation marks like "C:\work\a.log" (use @file for just the file name). If no file matches, forfiles displays an error message and returns exit code 1.
See
for details.Saving Search Results to a File
When there are many results, redirecting them to a text file makes them easier to review.
dir /s /b "C:\work\*.pdf" > result.txt
You can open the saved result.txt with notepad result.txt. A long list is easier to read in an editor than by scrolling through the screen.
For more about redirection, see
.Processing Found Files in a Batch File
If you want to do something with the files you found, you can use for /r.
@echo off
for /r "C:\work" %%f in (*.log) do (
echo %%f
)
When typing directly at the Command Prompt, write %f instead of %%f.
Combining for /f with dir lets you process, one by one, the results narrowed down by the dir options.
@echo off
for /f "delims=" %%f in ('dir /s /b /a-d "C:\work\*report*" 2^>nul') do (
echo Found: %%f
)
Leaving delims= empty makes each line be received as is, even if the path contains spaces. 2^>nul suppresses the error message shown when nothing is found.
Running explorer /select,"C:\work\2026\report.pdf" opens Explorer with that file selected.
Summary
- To search by file name, use
dir /s /b "folder\pattern" - To search by file content, use
findstr /s /i /m "text" pattern where /R folder patterncan also search recursively by file name- To narrow down by modified date, use
forfiles /D - Search results can be saved with
> result.txt, and processed one by one withfor /rorfor /f - The content of Word, Excel, and PDF files cannot be searched with
findstr