I've been working on some code in a batch file that evaluates two file dates. If one date is greater than the other then it runs another bat file. What I want to do is format the two dates as YYYYMMDD so that I can use the GTR
(greater than).
The code is below but and it works if I use ==
(equal) because it's evaluating the string. I only want to know if one file date is greater than the other file date.
I'm not asking for someone to amend the code below but if you can show me how to format the dates I would be very grateful.
set Fileone=File1.txt
set FileTwo=File2.txt
pushd "D:\Board\Broadcast\FA_Report8_A"
FOR %%f IN (%FileOne%) DO SET filedatetime=%%~tf
FOR %%f IN (%FileTwo%) DO SET filedatetime2=%%~tf
SET filedatetime2=%year%%month%%day%
IF %filedatetime:~0, 10% GTR %filedatetime2:~0, 10% (
echo FileOne Greater - run bat
timeout /t 20 /nobreak
goto Finish
) else (
echo FileOne not Greater - Finish
goto Finish
)
:Finish
echo finished
pause
The layout for date variable strings in the system can be assumed by settings from the user, by regional and/or language, so, date is not 100% predictable layout to work with.
Try using
wmic OS Get localdatetime /value
because the result is 100% predictable:SO, if you use in for loop, adding 2 delimiters like
=.
, (equal and dot), you go getting this string output:The layout from this command is predictable and works independent of regional settings, user settings or system language, so, the command will always return:
In bat file:
In command line:
This commands result ::
May I can also suggest:
In looping for:
Result:
The difference is no zero in number for month/day less equal 9, so, you can use this bat to put leading zero in this case:
Result in YearMonthDay:
Obs.: In PowerShell, the layout can be customized simple by:
The strings can be set with
ToString
.The Powershell command:
Result:
See more about date variable layout output in batch here:
So sorry by my limited English...
It's not portable between machines with different date formats but the simplest way is to use a substring:
%var:~STARTPOS,LENGTH%
You can separate a date in its parts with a FOR /F command:
This form prevents you to make a mistake in the position or size of each substring, and is very easy to change the order of the parts. For example, if your date is MM/DD/YYYY:
Type FOR /? for further details.