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
Batch file to list all files in a dir in windows 7
提问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 /b
switch (to write file list only, without header and other informations).
你必须使用/b
开关(只写文件列表,没有标题和其他信息)。
dir /b >> list.txt
Then if you need to list all .exe
files in c:\windows
the full command is:
那么如果你需要.exe
在c:\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 .jpg
and .png
files (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 /a
switch (to include/exclude items according to an attribute). In your case you want to exclude directories then you have to use -d
:
如果您需要排除目录,您可以依靠/a
switch(根据属性包含/排除项目)。在您的情况下,您想排除目录,则必须使用-d
:
dir /b /a-d >> list.txt
Finally do not forget dir
can be used recursively with /s
switch (to list files that match search pattern in the given directory and in all sub-directories) and sorted with /o
option. Use dir /?
for more details about that.
最后不要忘记dir
可以与/s
switch递归使用(列出在给定目录和所有子目录中匹配搜索模式的文件)并使用/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 existingdir /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"