windows 批处理文件列出Windows 7中目录中的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12847302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:58:26  来源:igfitidea点击:

Batch file to list all files in a dir in windows 7

windowsbatch-file

提问by A_Elric

I'm trying to list all the files in a directory and then write that to a text file using a batch file. I think it should be something like

我正在尝试列出目录中的所有文件,然后使用批处理文件将其写入文本文件。我认为它应该是这样的

dir / (some flags here) >> files.txt

which will contain a listing like

其中将包含一个列表,如

a.exe
b.exe
c.exe

etc. etc

等等等等

回答by Adriano Repetti

You have to use the /bswitch (to write file list only, without header and other informations).

你必须使用/b开关(只写文件列表,没有标题和其他信息)。

dir /b >> list.txt

Then if you need to list all .exefiles in c:\windowsthe full command is:

那么如果你需要.exec:\windows完整命令中列出所有文件是:

dir c:\windows\*.exe /b >> list.txt

Please note that >>will appendthe list to the file. Do not forget you can use multiple search patterns, for example:

请注意,这>>会将列表附加到文件中。不要忘记您可以使用多种搜索模式,例如:

dir *.jpg;*.png /b > list.txt

It'll write all the .jpgand .pngfiles (only names, without path or any other informations) to the file list.txt(overwriting the file if it did exist before).

它将所有.jpg.png文件(只有名称,没有路径或任何其他信息)写入文件list.txt(如果文件之前存在,则覆盖该文件)。

If you need to exclude directories you can rely on /aswitch (to include/exclude items according to an attribute). In your case you want to exclude directories then you have to use -d:

如果您需要排除目录,您可以依靠/aswitch(根据属性包含/排除项目)。在您的情况下,您想排除目录,则必须使用-d

dir /b /a-d >> list.txt

Finally do not forget dircan be used recursively with /sswitch (to list files that match search pattern in the given directory and in all sub-directories) and sorted with /ooption. Use dir /?for more details about that.

最后不要忘记dir可以与/sswitch递归使用(列出在给定目录和所有子目录中匹配搜索模式的文件)并使用/o选项排序。dir /?有关更多详细信息,请使用。

回答by SirOsis

You pretty much have it down already. if you are only wanting to list the files in that directory then

你已经差不多了。如果您只想列出该目录中的文件,那么

dir /A-D >> somefile.txt

OR

或者

dir /A-D > somefile.txt    

dir /
  • >>appends
  • >creates new or overwrites existing

    dir /A-D /ON > somefile.txt

  • >>附加
  • >创建新的或覆盖现有的

    dir /AD /ON > somefile.txt

This will sort by name.

这将按名称排序。

回答by Bali C

Here you go

干得好

dir C:\ /s /b >files.txt

回答by Andre

you can also use tree for recursive list files in directory tree

您还可以将树用于目录树中的递归列表文件

and for dumping to text file you can use this:

对于转储到文本文件,您可以使用:

C:\tree "D:\my directory" > "D:\dump file.txt"

C:\tree "D:\我的目录" > "D:\dump file.txt"