我可以使用CMD在Windows中搜索文件吗?


Answers:


36

在提示符(命令行)中输入:

dir /S /P "Path\FileName"

如果要将结果保存在文本文件中:

dir /S "Path\FileName" > "Path\ResultFilename"

3
如果文件是隐藏文件或系统文件,则可能还需要包含/ a参数!8
理查德·卢卡斯

更多信息:/s进行搜索,而显示/p一屏有价值的结果后的暂停。对于更多的选择,你可以做dir /?
路加福音

1
/s选项不会“进行搜索”。它扩展了dir命令(默认情况下会列出当前文件夹中的文件)以列出所有子文件夹中的所有文件。这扩大了dir命令的范围,可以绝对列出驱动器或文件夹中的所有内容,而不仅限于当前的内容。
Hashim

16

使用/b开关dir打印完整路径可能会有所帮助。说,C:\ > dir /b /s *file*.*

您仍然可以使用find或过滤结果for,然后使用将输出重定向到文件>filename


2

dir本来不是用于搜索文件而是用于列出目录,但是现在where可以用来搜索多种文件类型,例如 where /R c:\Users *.dll *.exe *.jpg

请检查完整的语法以及如何在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 
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.