0

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
hben
  • 65
  • 1
  • 1
  • 9

2 Answers2

2

There are the following problems:

  • do not use variable name date as this is a built-in variable containing the current date (type set /? for help);
  • the first for statement is useless, because %date% is already available;
  • the strings DATEONLY and date are compared literally in your if statement, you need to state %DATEONLY%==%date% instead;
  • the else statement must be in the same line as the closing parenthesis of the if body (type if /? for help);

So try this:

@ECHO OFF

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 (
ECHO     Wrong
)
PAUSE

Note: Regard that all those dates in the batch file are locale-dependent.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • so what is your date format when you enter `date /T` in the command prompt? is it the same as of `echo %Date%`? it should be... and what is the format of your `echo %FileDate%` value?? is it also the same (ignoring the time portion)?? – aschipfl Jul 28 '15 at 19:19
  • date/T is 07/29/2015, same as %date% – hben Jul 29 '15 at 16:35
  • echo %FileDate% value is 07/29/2015 11:20 AM – hben Jul 29 '15 at 16:36
  • in order to convert that to similar format, i have to add [set DATEONLY=%FileDate:~0,10%] – hben Jul 29 '15 at 16:37
  • yes, the line `set DATEONLY=%FileDate:~0,10%` truncates the time; but the line `for /f "tokens=1,2,3,4 delims=. " %%i in ('date /t') do set date=%%k%%j` has no effect, because you specify `. ` as delimiters but your date format uses `/`; so both `%%j` and `%%k` are empty and therefore the `for` body expands to `set date=`; the line `echo %date%` displays the date though because `%date%` is a built-in variable that (when not overwritten) expands to the current system date... – aschipfl Jul 29 '15 at 17:31
  • i just copy the code from some web, do you have anther way to get system date in 0730/2015 format? @aschipfl – hben Jul 30 '15 at 16:19
  • yes, just skip the said `for /f` line as it has no effect; `%date%` expands to the current date anyway... – aschipfl Jul 30 '15 at 16:41
  • i tested it, you can't skip for/f , because it will show Thur in front of the date, Thur is thursday – hben Jul 30 '15 at 20:21
  • I don't get it... where does this "Thur" come from? [above](http://stackoverflow.com/questions/31527032/batch-file-check-file-get-updated-to-todays-datesystem-date/31527372?noredirect=1#comment51350839_31527372) you said the output format of `date /T` is `07/30/2015`... are you testing on different machines with different region settings each time?? – aschipfl Jul 30 '15 at 23:56
0

Here is a completely different approach:

forfiles /P . /M MyFile.txt /D +0 /C "cmd /C echo @fdate @file"

The forfiles command is capable of checking the file date. In the above command line, it:

  • walks through the current directory (.),
  • lists all files named MyFile.txt (of course there is onlyone),
  • but only if it has been modified +0 days after today,
  • and then executed the command line after the /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 and ERRORLEVEL is set to 1.

Notice that forfiles is not a built-in command and might not be available on your operating system.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • I'd add this to your other answer instead of making a new answer, personally. – SomethingDark Jul 20 '15 at 23:29
  • @SomethingDark, I was thinking about that too but I found this answer is too different as it does not even use the code of the original question. I searched the Help Center to find a best-practice for such situations but without success. Maybe there are some more opinions?? – aschipfl Jul 21 '15 at 19:08
  • Thanks again! I just posted a new question related to this, I hope you can help too. – hben Jul 21 '15 at 19:13
  • @hben, I guess you are talking about [this](http://stackoverflow.com/q/31547576/5047996), right? – aschipfl Jul 21 '15 at 19:28
  • @aschipfl, http://stackoverflow.com/questions/31547576/batch-file-compare-3-strings-in-a-row-multiple-strings?noredirect=1#comment51054163_31547576 – hben Jul 21 '15 at 20:22