Formatting Modified Date Variable in Batch File
I'm creating a batch file that loops through files in my directory, executes a program and renames files based on modified date. I am able to set 'mydate'= to the modified date, but in order to append that date to the file name I need to format it mmddyyyy.
How do I format 'mydate' to mmddyyy? It currently reads mm/dd/yyyy hh:mm AM(PM)
Code:
ChDIR C:\Users\finanalyst1433\Desktop\New folder\Output\
SETLOCAL enabledelayedexpansion
FOR %%f IN (*.o_g) DO (
CALL C:\RTSimEXE\RTOutput.exe %cd%\%%f Month
set mydate=%%~Tf )
FOR %%f IN (*_Mon.csv) DO REN "%%f" "%%~nf_%mydate%%%~xf
SET deletestring=_Mon
FOR /f "delims==" %%F IN ('dir /b *.csv ^| FIND "%deletestring%"') DO (
SET oldfilename=%%F
SET newfilename=!oldfilename:%deletestring%=!
REN "!oldfilename!" "!newfilename!"
)
EXIT
Re: Formatting Modified Date Variable in Batch File
Check the Windows Batch File (.bat) to get current date in MMDDYYYY format.:
Code:
@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
echo %mydate%_%mytime%
If you prefer the time in 24hr/military format, you can replace the second FOR line with this:
Code:
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)