Searching for Files

Maintained on

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.

Search_by_file_name
dir /s /b "C:\work\*.pdf"
Search_by_file_content
cd /d "C:\work"
findstr /s /i /m "invoice" *.txt

Choosing a Search Method

What you want to findCommand to use
Search by file name or extensiondir /s /b
Search for several patterns at oncewhere /R
Search by text inside filesfindstr /s /i /m
Narrow down by modified dateforfiles /D
Process the files you found in a batchfor /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.

Find_PDFs_under_C:\work
dir /s /b "C:\work\*.pdf"
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /s /b "C:\work\*.pdf"
C:\work\invoice.pdf
C:\work\2026\report.pdf
C:\work\2026\archive\manual.pdf
C:\users\user>

If you only know part of the file name, put * before and after it.

Find_files_and_folders_whose_name_contains_report
dir /s /b "C:\work\*report*"
When dir finds nothing

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).

Find_folders_whose_name_contains_backup
dir /s /b /ad "C:\work\*backup*"

Finding Only Files

Adding /a-d excludes folders and limits the search to files.

Find_only_files
dir /s /b /a-d "C:\work\*report*"
*.htm can also match *.html

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.

Find_PDFs_whose_path_contains_2026
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.

Find_PDF_and_Word_files_together
where /R "C:\work" *.pdf *.docx
Type where.exe in PowerShell

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.

Find_"invoice"_in_text_files_under_C:\work
cd /d "C:\work"
findstr /s /i /m "invoice" *.txt
OptionMeaning
/sSearch the current folder (or the folder given as a path) and its subfolders
/iIgnore case
/mShow only file names, not the matching lines
/nAlso show the line number of each matching line

Without /m, the content of each matching line is displayed as well.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>findstr /s /i /n "error" *.log
app.log:12:2026-11-01 10:23:45 ERROR connection failed
logs\old.log:304:error code: 500
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

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

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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

Word, Excel, and PDF content cannot be searched

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.

Watch out for character encoding

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”.

Show_.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.

Save_the_search_results_to_result.txt
dir /s /b "C:\work\*.pdf" > result.txt
Review the results in Notepad

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.

Display_each_.log_file_under_C:\work
@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.

Process_the_results_narrowed_down_by_dir_one_by_one
@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.

Open the location of a found file in Explorer

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 pattern can 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 with for /r or for /f
  • The content of Word, Excel, and PDF files cannot be searched with findstr

Practice Questions

Practice Problem

Which command lists only the full paths of files with the .csv extension under C:\work, including subfolders?

Choose one answer.

  1. dir "C:\work\*.csv"
  2. dir /s /b "C:\work\*.csv"
  3. findstr /s ".csv" "C:\work"

Practice Problem

Which command shows only the names of text files under the current folder that contain “error”, ignoring case?

Choose one answer.

  1. dir /s /b error*.txt
  2. findstr /s /i /m "error" *.txt
  3. where /R . error
#Command Prompt #Batch File #File Search #dir #findstr #where