【问题标题】:Looping files in directory in reverse order以相反的顺序循环目录中的文件
【发布时间】:2013-09-15 22:51:43
【问题描述】:

在 DOS 批处理文件中,我可以编写以下循环以按字母升序迭代目录中的文件:

for %f in (*) do (echo %f)

我该如何做同样的事情,但以相反的顺序(字母降序)迭代文件?

【问题讨论】:

    标签: windows batch-file command-prompt


    【解决方案1】:

    从命令行:

    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="
    • @foxidrive - 阿门。我一直不明白为什么这么多人使用“TOKENS=*”,而实际上他们想要“DELIMS=”。
    • 简单的 FOR 循环只返回文件,但所写的 DIR 命令将返回文件和文件夹。应添加/A-D 选项以消除文件夹。要真正让它像简单的 FOR,它应该有 /A-D-H-S 来消除隐藏和系统文件。
    猜你喜欢
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 2012-07-18
    • 2013-05-29
    相关资源
    最近更新 更多