Page 1 of 1

If Date not equal to today then run a command??

Posted: 27 May 2013 03:44
by tbharber
Hey all,

I have a variable which has stored the modification date of a file in the format MM/DD/YYYY (ie. 05/27/2013) What I am trying to do is compare the date to today's, and if it is not the same then run a command. I am not sure how to do this because Windows stores the variable %date% as "Day MM/DD/YYYY" (ie. Mon 05/27/2013)

If I try to compare my variable to %DATE% it always comes back false.

example...

Code: Select all

if %myvar%=%date% echo "File has been updated today" else (Run Command Here)


I know this is super simple, I just do not know how to grab part of a variable. Any help would be awesome!

Re: If Date not equal to today then run a command??

Posted: 27 May 2013 03:54
by Endoro
you can try this:

Code: Select all

for /f "tokens=2" %%i in ("%date%") do set "cdate=%%i"
echo %cdate%

Re: If Date not equal to today then run a command??

Posted: 27 May 2013 04:47
by tbharber
That looks like it will work, but for whatever reason I cannot get it to say it is equal. The statement I gave above was a more simplified version of what I have. This is the code with your code included....

Code: Select all

for %%i in (
   scrfaxp5
   scrfaxp6
   srrfaxp5
   srrfaxp6
   ) do (
for /f "tokens=2" %%i in ("%date%") do set "cdate=%%i"
for /f %%a in ('dir \\%%i\d$\rightfax\Database\MaintAgingAndPurging.log ^|findstr MaintAgingAndPurging.log') do set
currentserverdate=%%a & if %%i==scrfaxp5 (
set server1=%%i last modified:!currentserverdate!
echo !server1!
if !currentserverdate!==!cdate! (echo Modified Today) else (echo Not Modified Today)
)
)



If I echo !currentserverdate! and !cdate! they both show as 05/27/2013, but for whatever reason when I test to see if they are equal in the IF statement, it doesn't work. It keeps Echoing the 2nd statement "Not Modified Today" even though they are both the same.

Re: If Date not equal to today then run a command??

Posted: 27 May 2013 05:00
by Endoro
try it with double quotes:

Code: Select all

do set "currentserverdate=%%a" & ....

Re: If Date not equal to today then run a command??

Posted: 27 May 2013 05:03
by tbharber
Yep that was it. Thanks a ton!