代码之家  ›  专栏  ›  技术社区  ›  Rhubbarb

如何比较批处理脚本中文件的时间戳?

  •  26
  • Rhubbarb  · 技术社区  · 16 年前

    在DOS批处理文件中,实现某些事情的方法有些模糊。幸运的是,有一个很棒的批处理脚本参考站点: Simon Sheppard's SS64 猛击 .)

    显而易见的 if exist "%dir%\*.*" 不起作用。但这可以通过以下条件执行技巧实现:

    ( dir /b /a "%dir%" | findstr . ) > nul && (
      echo %dir% non-empty
    ) || (
      echo %dir% empty
    )
    

    另一个棘手的问题是根据文件内容进行分支。

    ( fc /B "%file1%" "%file2%" | find "FC: no differences encountered" ) > nul && (
      echo "%file1%" and "%file2%" are the same
    ) || (
      echo "%file1%" and "%file2%" are different
    )
    

    所以,我的问题是:
    有没有办法根据文件的时间戳进行分支?

    这就是我想要的东西:

    REM *** pseudo-code!
    if "%file1%" is_newer_than "%file2%" (
      echo "%file1%" is newer
    ) else if "%file1%" is_older_than "%file2%" (
      echo "%file2%" is newer
    ) else (
      echo "%file1%" and "%file2%" are the same age
    )
    

    6 回复  |  直到 7 年前