【发布时间】:2013-09-15 22:51:43
【问题描述】:
在 DOS 批处理文件中,我可以编写以下循环以按字母升序迭代目录中的文件:
for %f in (*) do (echo %f)
我该如何做同样的事情,但以相反的顺序(字母降序)迭代文件?
【问题讨论】:
标签: windows batch-file command-prompt
在 DOS 批处理文件中,我可以编写以下循环以按字母升序迭代目录中的文件:
for %f in (*) do (echo %f)
我该如何做同样的事情,但以相反的顺序(字母降序)迭代文件?
【问题讨论】:
标签: windows batch-file command-prompt
从命令行:
for /f "tokens=*" %f in ('dir /b /o-n') do (echo %f)
在 bat 文件中:
for /f "tokens=*" %%f in ('dir /b /o-n') do (echo %%f)
/B Uses bare format (no heading information or summary).
/O List by files in sorted order.
sortorder N By name (alphabetic)
- Prefix to reverse order
Type "dir /?" in CMD for more details
【讨论】:
tokens=* 去除前导空格。如果文件名需要准确,最好使用"delims="。
/A-D 选项以消除文件夹。要真正让它像简单的 FOR,它应该有 /A-D-H-S 来消除隐藏和系统文件。