【问题标题】:Find latest file in a directory whose name starts with <> using batch script使用批处理脚本在名称以 <> 开头的目录中查找最新文件
【发布时间】:2015-06-27 03:30:06
【问题描述】:

所以我有一个创建日志文件的目录,我想读取最新的日志文件。该目录将包含日志文件、错误文件和其他一些文件,这些文件每次都会被创建。我的日志文件的名称以 test-install-.log

开头

如何使用批处理脚本找到最新的日志文件。

谢谢

【问题讨论】:

    标签: windows batch-file batch-processing


    【解决方案1】:
    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        set "lastLog="
        for /f "delims=" %%a in ('dir /b /o-d "x:\logsFolder\test-install-*.log" 2^>nul') do (
            if not defined lastLog set "lastLog=%%a"
        )
    
        echo %lastLog%
    

    或者,对于一长串文件,避免迭代

    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        set "lastLog="
        for /f "delims=" %%a in ('dir /b /o-d "x:\logsFolder\test-install-*.log" 2^>nul') do (
            set "lastLog=%%a" 
            goto :done
        )
    :done
        echo %lastLog%
    

    或者,如果你有一个非常大的文件列表

    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        set "lastLog="
        for /f "delims=" %%a in ('
            dir /b /o-d "x:\logsFolder\test-install-*.log" 2^>nul
            ^| cmd /q /v /c"set /p .=&if defined . (echo(!.!)"
        ') do set "lastLog=%%a" 
        echo %lastLog%
    

    【讨论】:

    • 太棒了..正是我想要的。谢谢@MC ND
    猜你喜欢
    • 2013-04-11
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多