28

I want a command to search for a file in a directory and all sub directories in windows using the command line. I saw all the available commands but I couldn't find any suitable command to do this operation.

3 Answers 3

38

At prompt (Command Line) type:

dir /S /P "Path\FileName"

If you want to save the results in a text file:

dir /S "Path\FileName" > "Path\ResultFilename"
3
  • 3
    If the file is hidden or a system file you might want to also include the /a parameter as well! 8-) Aug 18, 2010 at 7:28
  • A little more info: The /s does a search, whereas the /p pauses after a screen worth of results are shown. For more options you can do dir /?.
    – Luke
    Aug 11, 2016 at 20:37
  • 2
    The /s option doesn't "do a search". It extends the dir command - which lists the files in the current folder by default - to list all those in any subfolders, too. This expands the dir command's scope to list absolutely everything in a drive or folder rather than being limited to the current one. Jun 6, 2017 at 3:57
16

use the /b switch to dir to print full path might be helpful. say, C:\ > dir /b /s *file*.*

still, you can filter the result with find or for, and redirect output to file with >filename

5

dir was not meant for searching files but to list directories, but now there is where which can be used to search multiple file types as where /R c:\Users *.dll *.exe *.jpg

do check the full syntax and the answer for How to do a simple file search in cmd

WHERE [/R dir] [/Q] [/F] [/T] pattern...

Description:
    Displays the location of files that match the search pattern.
    By default, the search is done along the current directory and
    in the paths specified by the PATH environment variable.

Parameter List:
    /R       Recursively searches and displays the files that match the
             given pattern starting from the specified directory.

    /Q       Returns only the exit code, without displaying the list
             of matched files. (Quiet mode)

    /F       Displays the matched filename in double quotes.

    /T       Displays the file size, last modified date and time for all
             matched files.

    pattern  Specifies the search pattern for the files to match.
             Wildcards * and ? can be used in the pattern. The
             "$env:pattern" and "path:pattern" formats can also be
             specified, where "env" is an environment variable and
             the search is done in the specified paths of the "env"
             environment variable. These formats should not be used
             with /R. The search is also done by appending the
             extensions of the PATHEXT variable to the pattern.

     /?      Displays this help message.

  NOTE: The tool returns an error level of 0 if the search is
        successful, of 1 if the search is unsuccessful and
        of 2 for failures or errors.

Examples:
    WHERE /?
    WHERE myfilename1 myfile????.*
    WHERE $windir:*.* 
    WHERE /R c:\windows *.exe *.dll *.bat  
    WHERE /Q ??.??? 
    WHERE "c:\windows;c:\windows\system32:*.dll"
    WHERE /F /T *.dll 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .