【问题标题】:get two recent files from a folder in a batch script从批处理脚本中的文件夹中获取两个最近的文件
【发布时间】:2020-02-06 09:11:57
【问题描述】:

我正在尝试编写一个批处理脚本,该脚本根据给定目录的创建时间获取两个最近的文件。在这个批处理脚本中,我想调用一个将这两个文件作为参数的 .exe 文件。

有人可以帮我解决这个问题吗?

dir /a:-d-s /b /o:-d /t:c

这是我使用的命令,它将根据创建时间按降序列出文件的名称。该命令将结果打印到控制台,但我想将其分配给某个变量或数组并访问前两个文件名。

【问题讨论】:

    标签: batch-file command-line scripting


    【解决方案1】:

    以下示例旨在将两个最上面的返回文件设置为“数组类型”变量%Newest[1]%%Newest[2]%

    @For /F "Delims==" %%A In ('Set Newest[ 2^>NUL')Do @Set "%%A="
    @For /F "Tokens=1*Delims=:" %%A In ('Dir /B/A-D-S/O-D/TC 2^>NUL^|FindStr /N "^"'
    )Do @If %%A LEq 2 (Set "Newest[%%A]=%%B")Else GoTo Next
    :Next
    @Set Newest[ 2>NUL&&Pause
    

    最后一行只是为了向您展示从循环中创建的所有变量。您显然会用您自己的代码替换这一行,或者保持原样,并将您的代码放在它下面。如果您想使用最近修改的日期和时间戳而不是创建的,请将/TC 替换为/TW 在线2


    题外话: 这是一个使用中的示例,基于您后来发布的问题,因为已删除:
    @Echo Off
    For /F "Delims==" %%A In ('Set Newest 2^>NUL')Do Set "%%A="
    For /F "Tokens=1-2*Delims=:" %%A In ('Dir /B/A-D-S/O-D/TC "Checkpoints" 2^>NUL^
     ^|FindStr /N "^"')Do If %%A LEq 2 (Set "Newest%%A=%%B")Else GoTo Next
    :Next
    Set "DT1="&For /F "Tokens=1*Delims=_" %%A In ('Set Newest 2^>NUL'
    )Do If Not Defined DT1 (Set "DT1=%%~nB")Else Set "DT2=%%~nB" 
    If Not Defined DT1 Exit /B
    Set "Target=Differences\Difference_%DT1%_%DT2%.csv"
    Set Newest 2>NUL
    Set Target 2>NUL
    If Not Exist "Differences\" MD "Differences"
    Pause
    

    【讨论】:

    • 谢谢。这行得通。但是当我尝试访问 Newest[1] 和 Newest[2] 的子字符串时,它似乎什么也没返回。这是使用的代码。设置目标=.\Differences\Difference_%Newest[2]~11,36%_%Newest[1]~11,36%.csv。回显时,Newest[1] 和 Newest[2] 都会导致长度超过 50 的字符串。但这里的目标是 .\Differences\Difference__.csv。可能是什么问题?
    • 如果您向我展示这两个变量值,以及您希望命令行是什么样子,我会尽力提供帮助。但请注意,这个问题超出了问题的范围,这个答案,因为你想要的变量,这正是这样做的。如有必要,请将其作为格式化代码发布在您的问题正文中,以免评论区难以阅读。可能只是使用Set "Newest[%%A]=%%~nB"Set "Newest[%%A]=%%~nxB" 会更好。
    • @seshasaisuhashdesu,请看一下我在上面发布的代码作为题外话的答案。这是对您之前发布的问题的回应,但已被删除。希望对您有所帮助。
    【解决方案2】:

    这是此任务的注释批处理文件。

    @echo off
    rem Set up a local environment for this batch file with enabled command
    rem extensions as required for command FOR and with disabled delayed
    rem environment variable expansion to process also correct file names
    rem with one or more exclamation marks in name.
    setlocal EnableExtensions DisableDelayedExpansion
    
    rem Make directory of the batch file the current directory.
    rem It is possible to use any other directory on replacing
    rem %~dp0 by a directory path.
    pushd "%~dp0"
    
    rem Make sure the environment variable File1 is not defined.
    set "File1="
    
    rem Get a list of files without system attribute in current directory
    rem ordered by creation date with newest first and oldest last and process
    rem this list of file names line by line with ignoring the batch file. The
    rem loop processing the file names is exited after having assigned second
    rem newest created file in this directory to the environment variable File2
    rem in addition to newest file assigned to environment variable File1 with
    rem a jump to the command line below the line with label RunProgram.
    for /F "eol=| delims=" %%I in ('dir /A-D-S /B /O-D /TC 2^>nul') do (
        if not "%%I" == "%~nx0" (
            if not defined File1 (
                set "File1=%%I"
            ) else (
                set "File2=%%I"
                goto RunProgram
            )
        )
    )
    
    rem There is no file or just one file found in current directory other
    rem than this batch file and so the program cannot be executed at all.
    goto EndBatch
    
    rem Run the program with the file names of the two newest
    rem created files in current directory (batch file directory).
    :RunProgram
    "C:\Path To Application\Application.exe" "%File1%" "%File2%"
    
    rem Restore the initial current directory and also the initial environment
    rem variables list on starting of this batch file as well as initial state
    rem of command extensions and delayed environment variable expansion.
    :EndBatch
    popd
    endlocal
    

    要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

    • call /? ... 解释%~dp0(批处理文件的驱动器和路径)和%~nx0(批处理文件的名称和扩展名)。
    • dir /?
    • echo /?
    • endlocal /?
    • for /?
    • goto /?
    • if /?
    • popd /?
    • pushd /?
    • rem /?
    • set /?
    • setlocal /?

    阅读有关Using command redirection operators 的Microsoft 文章,了解2>nul 的解释。重定向运算符 > 必须在 FOR 命令行上使用插入字符 ^ 转义,以便在 Windows 命令解释器在执行命令 FOR 之前处理此命令行时解释为文字字符> 它在后台启动的单独命令进程中执行嵌入的dir 命令行,并附加了指定的命令行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多