I want to create a batch to check if the file have been modified to today's date, what i did was to "bring in a system's date and compare it with the modified date, if they match, then trigger something. My batch file works well and displays two right dates, but the IF statement saying the date mismatch.
@ECHO OFF
for /f "tokens=1,2,3,4 delims=. " %%i in ('date /t') do set date=%%k%%j
echo %date%
pause
FOR %%a IN (D:\MyFile.txt) DO SET FileDate=%%~ta
set DATEONLY=%FileDate:~0,10%
echo %DATEONLY%
pause
if DATEONLY==date (
echo date ok
)
else (
cls
ECHO Wrong
)
PAUSE
There are the following problems:
date
as this is a built-in variable containing the current date (typeset /?
for help);for
statement is useless, because%date%
is already available;DATEONLY
anddate
are compared literally in yourif
statement, you need to state%DATEONLY%==%date%
instead;else
statement must be in the same line as the closing parenthesis of theif
body (typeif /?
for help);So try this:
Note: Regard that all those dates in the batch file are locale-dependent.
Here is a completely different approach:
The
forfiles
command is capable of checking the file date. In the above command line, it:.
),MyFile.txt
(of course there is onlyone),+0
days after today,/C
switch.If
MyFile.txt
has been modified today (or even in future), the given command line is executed; if it has been modified earlier than today, an error message is displayed andERRORLEVEL
is set to1
.Notice that
forfiles
is not a built-in command and might not be available on your operating system.