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):
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 woul开发者_如何学运维d greatly appreciate any feedback/help on this. Thanks.
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 %%~tf
would 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.
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"
.
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.
For example: to list all files that have been modified in the last 2 days:
forfiles /D -2 /C "cmd /c ECHO file selected...@path, dated @fdate"
精彩评论