Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
tbharber
- Posts: 32
- Joined: 17 Sep 2010 17:08
#1
Post
by tbharber » 27 May 2013 03:44
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!
-
Endoro
- Posts: 244
- Joined: 27 Mar 2013 01:29
- Location: Bozen
#2
Post
by Endoro » 27 May 2013 03:54
you can try this:
Code: Select all
for /f "tokens=2" %%i in ("%date%") do set "cdate=%%i"
echo %cdate%
-
tbharber
- Posts: 32
- Joined: 17 Sep 2010 17:08
#3
Post
by tbharber » 27 May 2013 04:47
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.
-
Endoro
- Posts: 244
- Joined: 27 Mar 2013 01:29
- Location: Bozen
#4
Post
by Endoro » 27 May 2013 05:00
try it with double quotes:
Code: Select all
do set "currentserverdate=%%a" & ....