Results 1 to 2 of 2

Thread: Formatting Modified Date Variable in Batch File

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    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

  2. #2
    Join Date
    May 2008
    Posts
    3,518

    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)

Similar Threads

  1. Replies: 3
    Last Post: 12-01-2012, 07:48 AM
  2. Windows batch file: set output of program to a variable?
    By supernoob in forum Windows Software
    Replies: 5
    Last Post: 17-10-2010, 05:58 AM
  3. How do i modified pst file in Outlook
    By Atilla in forum Windows Software
    Replies: 4
    Last Post: 30-04-2009, 10:53 AM
  4. Date and Time format in a BATCH File
    By RogerFielden in forum Software Development
    Replies: 3
    Last Post: 20-04-2009, 11:47 AM
  5. Batch file to create directory with a date in Windows 2003
    By Aadimoolan in forum Window 2000 Help
    Replies: 2
    Last Post: 16-11-2007, 01:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •