• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2082
  • Last Modified:

Date file modified for comparison

I have the below batch file to compare the date of a flag file to the system date and shut down a program if the flag file is the same date as the system date, or check to see if the flag file has been updated 10 minutes later.

The IF line exits the process.  When I ECHO the whole batch file, it looks like dt is taking its value from the contents of the file, rather than from the attributes.  How do I get the modified date of the file, or cast a string to a date?

REM @echo off

REM set variable to flag file mod date in sys format
for /f "tokens=1,2" %%i in (C:\Admin\flag.txt) do (
   set dt=%%i %%j
REM echo %dt%
REM compare flag mod date to sys date
if %dt% equ %date% GOTO indexer
echo Not Equal
REM dates not same so wait 10 minutes and try again
ping -n 601 localhost > nul
GOTO loop

echo Equal
REM dates same so close Indexer
CALL C:\Admin\EndIndex.bat
REM wait for fileserver to come back up
ping -n 11 localhost > nul
REM start Indexer again
CALL C:\Admin\StartIndex.bat

  • 2
  • 2
1 Solution
Your FOR /F is looking at the contents of the file C:\Admin\flag.txt.

Try this:

REM set variable to flag file mod date/time in sys format
for %%i in (C:\Admin\flag.txt) do set dt=%%~ti
REM set variable to date only
for /f "tokens=1" %%i in ('echo %dt%') do set dt=%%i

Open in new window

NCHCITAuthor Commented:
Thank you.  You've answered my question, but my problem still exists.  The program exits at the IF line.
>> The program exits at the IF line.

Presumably with an error message, which you haven't told me.

That may depend on how you format your date. Try

if "%dt%" equ "%date%" GOTO indexer
NCHCITAuthor Commented:
This novice got a helpful hint - run the batch file from a command prompt rather than executing it through Windows.  That gave me the error, and in turn I was able to resolve the issue (the space in the ddd mm/dd/yyyy was causing issues).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now