Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Batch file.. produce date/time in filename firendly format.

Options
  • 10-07-2010 1:06am
    #1
    Registered Users Posts: 2,234 ✭✭✭


    After trawling through the web for the last two hours i'm giving up..and desperate now!

    I need some windows batch code this will produce a timestamp in the following format:

    dd_mm_yyy-hh_mm (24hr)

    I've been reading tutorials and playing with various code samples for the past two hours and have come up with nothing..

    Any input greatly appreciated..thanks.


Comments

  • Registered Users Posts: 1,916 ✭✭✭ronivek


    I'm assuming this isn't homework as it's July; so here you go.

    [PHP]
    @echo off

    setlocal
    for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set date_string=%%a_%%b_%%c)
    for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set time_string=%%a_%%b)

    echo %date_string%-%time_string%
    endlocal
    [/PHP]

    The output is:
    10_07_2010-13_11


  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    Do you have to use batch? I'd recommend Powershell for windows scripting these days, in which case it'd just be:
    Get-Date -UFormat "%d_%m_%Y-%H_%M"
    


  • Registered Users Posts: 1,228 ✭✭✭carveone


    ronivek wrote: »
    I'm assuming this isn't homework as it's July; so here you go.

    Nice one ronivek! Never underestimate the power of the Windows for command to perform entirely dubious but interesting tricks.

    As an interesting addendum: for those who don't know, you can do terrifying things (terrifying for old Dos hands like me that is) like call batch subroutines within a for command (do a "call /?" to see call with a label), enabling you to do at least three impossible things before lunch :)


Advertisement