Format file date YYYYMMDD in batch

batch-file, cmd, windows

Solution

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"

Problem

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 ```

Original source