Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format file date YYYYMMDD in batch

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
like image 382
Cidr Avatar asked Feb 18 '26 04:02

Cidr


2 Answers

It's not portable between machines with different date formats but the simplest way is to use a substring: %var:~STARTPOS,LENGTH%

set filedatetime=14/06/2012 12:26
set filedatetime=%filedatetime:~6,4%%filedatetime:~3,2%%filedatetime:~0,2%
echo "%filedatetime%"

"20120614"
like image 132
Alex K. Avatar answered Feb 21 '26 14:02

Alex K.


You can separate a date in its parts with a FOR /F command:

set filedatetime=14/06/2012 12:26
for /F "tokens=1-3 delims=/ " %%a in ("%filedatetime%") do (
   set filedatetime=%%c%%b%%a
)

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:

set filedatetime=%%c%%a%%b

Type FOR /? for further details.

like image 36
Aacini Avatar answered Feb 21 '26 14:02

Aacini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!