Generate unique file name with timestamp in batch script

前端 未结 7 1177
隐瞒了意图╮
隐瞒了意图╮ 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:46

    I use this to create a unique file name for the execution of the batch file.

    REM ****Set up Logging ****
    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%") do (set mytime=%%a%%b)
    set mytime=%mytime: =0%
    
    set Logname="PCCU_%mydate%_%mytime%.log"
    Echo.  >>%Logname% 2>>&1
    Echo.=================== >>%Logname% 2>>&1
    

    I had to add the line

    set mytime=%mytime: =0%
    

    because I had the same problem where a blank was being entered before 10 AM, now I get 09 instead of 9. I also reuse the %mydate% and %mytime% variable for other files that I create with this script so that they all have the same date time stamp.

    0 讨论(0)
提交回复
热议问题