Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm creating a backup script in a .bat file in Windows 7. As part of the script I create a log file. I'd like to create the log file with the following date format: YYYYDDMM_HH24MISS_backup.log

Any ideas on how to do this?

share|improve this question

5 Answers 5

substring fun!

set filename=%date:~6,4%%date:~0,2%%date:~3,2%_%time:~0,2%24%time:~3,2%%time:~6,2%_backup.log

then just append data to it in the script:

echo "some new data" >> %filename%
ipconfig /all >> %filename%

output filename example:

20092810_23242544_backup.log

not sure if you want that 24 hardcoded after the hours in HH 24.

share|improve this answer
2  
Remember, this only works for some locales and fails miserably for others (or mis-interprets some fields). You can see here robvanderwoude.com/datetimentparse.php for some more robust ways of doing this. –  Joey Oct 29 '09 at 7:18

Thanks for all the help! Using all the information you posted here's what I finally used:

REM SET YEAR
set YEAR=%date:~6,4%

REM SET MONTH
set MONTH=%date:~3,2%
if %MONTH% LSS 10 set MONTH=%MONTH:~1,2%
if %MONTH% LSS 10 set MONTH=0%MONTH%

REM SET DAY
set DAY=%date:~0,2%
if %DAY% LSS 10 set DAY=%DAY:~1,2%
if %DAY% LSS 10 set DAY=0%DAY%

REM SET HOUR
set HOUR=%time:~0,2%
if %HOUR% LSS 10 set HOUR=%HOUR:~1,2%
if %HOUR% LSS 10 set HOUR=0%HOUR%

REM SET MINUTE
set MINUTE=%time:~3,2%
if %MINUTE% LSS 10 set MINUTE=%MINUTE:~1,2%
if %MINUTE% LSS 10 set MINUTE=0%MINUTE%

REM SET SECOND
set SECOND=%time:~6,2%
if %SECOND% LSS 10 set SECOND=%SECOND:~1,2%
if %SECOND% LSS 10 set SECOND=0%SECOND%

set filename=%YEAR%%MONTH%%DAY%_%HOUR%%MINUTE%%SECOND%.log

I tried to upvote both your answers but I didn't have enough reputation...

share|improve this answer

The other answer will choke when the month or hour are less than 10.

REM get a date time for a logfile name
REM If the echo %time% does not return a 24 hr time on the target OS / region settings,
REM You will need to do something like 
REM    set %ampm%=%time:~9,2%
REM    if "%ampm%" EQU "PM" set /a hour=hour+12

REM testcase 1 with months, hours, mins, etc, less than 10.  
REM Make sure to test with a format matches what your OS/regional settings will produce 
REM test data should match the output from  "for /f "tokens=1,2" %%u in ('date /t') do set d=%%v"
REM and for /f "tokens=1" %%u in ('echo %time%') do set t=%%u

REM set d=01/02/2009
REM set t=3:04:05.06

REM testscase 2 with full width month, hours, mins, etc.
REM Make sure to test with a format matches what your OS/regional settings would produce in this case.

REM set d=10/20/2009
REM set t=10:20:30.40

REM if not testing, use the real date and time:
REM 
REM the next line grabs the second (space) delimited thing from 'date /t', trims up spaces, and stores it in d
for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
REM this times up the spaces from 'time'
for /f "tokens=1" %%u in ('echo %time%') do set t=%%u
if "%t:~1,1%"==":" set t=0%t%

echo d has the value: "%d%"
echo t has the value: "%t%"

REM @echo off
set hour=%t:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
set min=%t:~3,2%
set secs=%t:~6,2%

set year=%d:~-4%
set month=%d:~0,2%
set day=%d:~3,2%

echo year=%year%,month=%month%,day=%day%,hour=%hour%,min=%min%,secs=%secs%

set datetimePartOfFile=%year%%day%%month%_%hour%24%min%%secs%
echo %datetimePartOfFile%

set filename=%datetimePartOfFile%_backup.log
echo %filename%

ren logfile.log %filename%

You can strip out a lot of it once you are sure it works, and have tested with months and hours less than 10, and your regional settings.

Here's the stripped down version without comments, or test cases:

Again, check that it works with your regional settings.

for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
for /f "tokens=1" %%u in ('echo %time%') do set t=%%u
if "%t:~1,1%"==":" set t=0%t%
set hour=%t:~0,2%
set min=%t:~3,2%
set secs=%t:~6,2%
set year=%d:~-4%
set month=%d:~0,2%
set day=%d:~3,2%
set datetimePartOfFile=%year%%day%%month%_%hour%24%min%%secs%
set filename=%datetimePartOfFile%_backup.log
ren logfile.log %filename%
share|improve this answer

The best solution I found reading all around the internet is below (usefull for backup filename creation (YYYYMMDD_HHMISS)

@echo off

set DT1=%date:~6,4%%date:~3,2%%date:~0,2%
set TIME1=%time:~0,8%
set TIME1=%TIME1::=%

set FILENAME1=%DT1%_%TIME1%
@echo test %FILENAME1%
share|improve this answer
@echo off

set fileName=Nome do seu arquivo

set extension=xls

set destination=C:/Users/Chunda/Dropbox

set ano=%date:~6,4%
set mes=%date:~3,2%
set dia=%date:~0,2%

set hora=%time:~0,2%
set minuto=%time:~3,2%
set segundo=%time:~6,2%

set backupName=_%ano%-%mes%-%dia%_%hora%h%minuto%m%segundo%s

set commandString="%fileName%.%extension%" "%destination%%fileName%%backupName%.%extension%"

copy %commandString%

echo %fileName%.%extension%

pause
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.