Windows batch file to check the modified file date and output to the log file, if not specified

I need to register systems that do not have a specific file in a specific folder, and created the next batch, which works fine. It will be called when entering the script domain (clients are Windows XP in the AD AD domain):

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 certain modified date, and if not, output it to the log file. While I'm at a standstill, I really appreciate any feedback / help on this. Thanks.

+4
source share
2 answers

You can get information about the date and time the file was changed in the script package, but you need to remember the following things:

  • It is a combination of date and time;
  • it is locale specific;
  • this is a string.

This means that before the comparison, you will need to disable the temporary part, for which you will need to take into account the display format, as indicated in the regional settings of the system. And since this is a string, you can probably only check if it is a specific date, but whether it refers to a specific period.

And here is how you can implement this:

 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, %%~tf will return the date and time formatted as dd.MM.yyyy hh:mm . Thus, the %filedatetime:~0,-6% follows this format and accordingly reduces the time part. You may need to slightly modify the expression to suit your case.

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

+7
source

If the date is for today (for example, a file has been updated in the last 7 days), you can use the "forfiles" command, which has built-in date calculations.

For example: to view all files that have been changed in the last 2 days:

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

Source: https://habr.com/ru/post/1342788/


All Articles