Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

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

  • 10-07-2010 01:06AM
    #1
    Registered Users, Registered Users 2 Posts: 2,238 ✭✭✭


    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, Registered Users 2 Posts: 1,924 ✭✭✭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, Registered Users 2 Posts: 1,346 ✭✭✭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
Advertisement