Windows 批处理文件检查文件的修改日期,如果不是特定值则输出到日志文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5231063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:09:37  来源:igfitidea点击:

Windows Batch file to check modified date of file and output to log file if not a specific value

windowsbatch-file

提问by steve

I am need to log systems that do not have a specific file in a specific folder and have created the below batch which works fine. It will be called by the domain logon script (Clients are Windows XP in a 2003 AD domain):

我需要记录在特定文件夹中没有特定文件的系统,并创建了以下可以正常工作的批处理。它将被域登录脚本调用(客户端是 2003 AD 域中的 Windows XP):

IF EXIST "C:\Documents and Settings\%username%\Application Data\Microsoft\Outlook\test.OTM" (
goto END
) ELSE (
echo %DATE%_%TIME%_%COMPUTERNAME%  >> %LOG1%
)

In addition to this, however, if the file is present I need to check that it has a specific modified date and if not then output it to a log file. So far I am stumped and would greatly appreciate any feedback/help on this. Thanks.

然而,除此之外,如果文件存在,我需要检查它是否具有特定的修改日期,如果没有,则将其输出到日志文件。到目前为止,我很难过,非常感谢对此的任何反馈/帮助。谢谢。

回答by Andriy M

You can obtain information about the file's modification date and time in a batch script, but you'll need to remember these things:

您可以在批处理脚本中获取有关文件修改日期和时间的信息,但您需要记住以下几点:

  • it comes as a combination of date and time;
  • it's locale specific;
  • it's a string.
  • 它是日期和时间的组合;
  • 它是特定于语言环境的;
  • 它是一个字符串

That means that before comparing you'll need to cut off the time part, for which you'll need to take into account the display format as specified in the system's regional settings. And because it's a string, you'll probably be only able to check whether it is a specific date, but not whether it belongs to a specific period.

这意味着在比较之前,您需要切断时间部分,为此您需要考虑系统区域设置中指定的显示格式。而且因为它是一个字符串,所以您可能只能检查它是否是特定日期,而不能检查它是否属于特定时期。

And here's how you can have it implemented:

这是实现它的方法:

SET filename="C:\Documents and Settings\%username%\Application Data\Microsoft\Outlook\test.OTM"
IF NOT EXIST %filename% GOTO log
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
IF "%filedatetime:~0,-6%" == "%checkdate%" GOTO END
:log
ECHO %DATE%_%TIME%_%COMPUTERNAME%  >> %LOG1%

On my system %%~tfwould return the date and time formatted as dd.MM.yyyy hh:mm. So the %filedatetime:~0,-6%part follows that format and cuts off the time part accordingly. You may need to change the expression slightly to fit your case.

在我的系统%%~tf上将返回格式为dd.MM.yyyy hh:mm. 因此该%filedatetime:~0,-6%部分遵循该格式并相应地切断时间部分。您可能需要稍微更改表达式以适合您的情况。

One last thing is, there's a predefined variable called USERPROFILE. It points to the active user's 'home' folder, C:\Documents and Settings\username. So you can reduce the path string to this: "%USERPROFILE%\Application Data\Microsoft\Outlook\test.OTM".

最后一件事是,有一个名为 的预定义变量USERPROFILE。它指向活动用户的“home”文件夹,. 因此,您可以将路径字符串简化为:。C:\Documents and Settings\username"%USERPROFILE%\Application Data\Microsoft\Outlook\test.OTM"

回答by Mark

If the date is relative to today (e.g. file updated within the last 7 days) you can use the "forfiles" command which has date calculations built in.

如果日期相对于今天(例如,文件在过去 7 天内更新),您可以使用内置日期计算的“forfiles”命令。

For example: to list all files that have been modified in the last 2 days:

例如:要列出最近 2 天内修改过的所有文件:

forfiles /D -2 /C "cmd /c ECHO file selected...@path, dated @fdate"