Ora

How to Find a File in Windows 10 Command Line

Published in Windows Command Line File Search 5 mins read

You can efficiently locate files in Windows 10 using the command line, primarily through the dir command, which allows you to search by name or pattern within specified directories and their subdirectories. This method provides powerful, flexible, and scriptable ways to discover files.

The dir Command: Your Primary Tool for File Searching

The dir command (short for directory) is the most common and versatile command-line utility for listing files and subdirectories. When combined with specific switches and wildcards, it becomes a powerful search tool.

Searching by Name and Pattern

To find files using the dir command by name, you can specify the filename or a pattern. A key switch for searching beyond the current directory is /s, which instructs dir to search through all subdirectories.

Basic Syntax for Searching:

dir [path\][filename] [/s] [/b] [/a[[:]attributes]] [...]
  • [path\]: The directory where the search should begin. If omitted, it defaults to the current directory.
  • [filename]: The specific file name or pattern you are looking for.
  • /s: This crucial switch tells dir to search the current directory and all its subdirectories.

Example from Reference:

To find files with a specific name, such as document.txt, you might use:

dir document.txt /s

If you are unsure of the exact extension or want to find all files starting with "report," you can use wildcards:

dir report.* /s

This command will search for any file named "report" with any extension (.*) in the current directory and all its subdirectories.

Understanding Wildcards

Wildcards are special characters that represent one or more other characters, making your searches more flexible:

  • *`` (Asterisk):** Represents any sequence of zero or more characters.
    • *.txt: All files with a .txt extension.
    • data*: All files starting with "data".
    • *report.docx: All files ending with "report.docx".
  • ? (Question Mark): Represents any single character.
    • file?.txt: Matches file1.txt, fileA.txt, but not file10.txt.

Essential dir Command Switches for Searching

Switch Description Example Command
/s Searches the current directory and all its subdirectories for the specified files. dir my_photo.jpg /s
/b Uses bare format (no heading information or summary). Only lists the full path and filename. dir *.log /s /b
/a[[:]attributes] Displays files with specified attributes. Attributes can be:
d (directories), h (hidden files), r (read-only files), a (files ready for archiving), s (system files), i (not content indexed files), l (reparse points). Prepending - excludes files with that attribute.
dir *.txt /s /ah (Finds hidden .txt files)
/p Pauses after each screenful of information, useful for large results. dir *.exe /s /p
/o[[:]sortorder] Lists files in sorted order. Sort orders can be:
n (name), s (size), e (extension), d (date/time), g (group directories first). Prepending - reverses the order.
dir /s /o-d (Lists files by newest first)
/w Uses wide list format, listing files in columns. dir /s /w

Practical Examples for Finding Files

Here are some common scenarios for finding files using the dir command:

  1. Find a specific file anywhere on the C: drive:

    dir C:\MyImportantDocument.docx /s

    This command starts searching from the root of the C: drive and looks in all subdirectories.

  2. Find all .log files in your Documents folder and its subfolders:

    dir C:\Users\%USERNAME%\Documents\*.log /s

    Replace C:\Users\%USERNAME%\Documents\ with your actual Documents path if it differs.

  3. Find all files starting with "invoice" with any extension:

    dir invoice*.* /s

    If executed from C:\, it searches the entire C: drive. If executed from a specific folder, it searches that folder and its subfolders.

  4. Find all hidden .ini files on your D: drive:

    dir D:\*.ini /s /ah

    The /ah switch specifically looks for files with the hidden attribute.

  5. List only the file paths of all .pdf files in a directory and its subdirectories:

    dir C:\Projects\*.pdf /s /b

    The /b switch ensures only the file paths are displayed, without extra dir output information.

Locating Executables with the where Command

While dir is excellent for general file searching, the where command is specifically designed to locate executable files, scripts, and other files that are found within your system's PATH environment variable. It functions similarly to the which command in Unix-like systems.

Syntax:

where [options] <pattern>

Example:

To find the location of the notepad.exe executable:

where notepad.exe

This will typically return C:\Windows\System32\notepad.exe.

Advanced Search Techniques

Redirecting Search Results to a File

For extensive searches, redirecting the output to a text file can be very useful for review and further processing.

dir C:\MyData\*.xlsx /s /b > found_excel_files.txt

This command will save a list of all Excel files found in C:\MyData\ and its subfolders (full paths only, thanks to /b) into a text file named found_excel_files.txt in your current directory.

Best Practices for Command Line Searching

  • Specify the Starting Directory: Always be mindful of where your search begins. If you want to search an entire drive, start the path with C:\ or D:\. If you want to search from your current location, omit the path or use . for the current directory.
  • Use Wildcards Wisely: * and ? are powerful. Be specific enough to narrow down results but broad enough to catch variations.
  • Permissions: Some directories (like C:\Windows\System32\config) might be protected. You might need to run your command prompt as an administrator to search these locations without "Access Denied" errors. To do this, right-click on the Command Prompt shortcut and select "Run as administrator."
  • Combine Switches: Don't hesitate to combine multiple switches (e.g., /s /b /ah) to refine your search precisely.

By mastering the dir command and its switches, you gain a powerful, efficient, and flexible way to find any file on your Windows 10 system using the command line.