Generate unique file name with timestamp in batch script

前端 未结 7 1189
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 13:07

In my .bat file I want to generate a unique name for files/directories based on date-time.

e.g.

Build-2009-10-29-10-59-00

The probl

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 13:28

    To make the code in the previous example work, I needed to adjust slightly. I presume this is because my PC is using a UK date format. To get back "2014-04-19" in mydate I needed:

    For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%b-%%a)
    

    Below I've pasted my total script, and included an example of how to use the filename

    For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%b-%%a)
    For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
    set mytime=%mytime: =0%
    
    set Logname="c:\temp\LogFiles\MyLogFile_%mydate%_%mytime%.log"
    Robocopy  \\sourceserver\Music H:\MyBackups\Music /MIR /FFT /Z /XA:H /W:5 /np /fp /dcopy:T /unilog:%Logname% /tee 
    

    Hope this helps!

提交回复
热议问题