I am running a program from a batch file, which when it is done performs an automatic backup of my MySQL database.

I would like the batch file to create a different back up for each run, so I can backtrace.

The desired filename would be gnucash_shockwave-20121128210344.sql (Date format YYYY-MM-DD-HH-MM-SS)

I have googled a few things that said try %DATE:~4% and %Date.Year% but I get an error that says The system cannot find the specified path.

If I remove the attempt to timestamp it, the script works fine, but over writes the previous backup

Here is the section of code I'm talking about:

@REM *** EXECUTION *** echo. Starting backup... SET timestamp %DATE:~-4%%DATE:~4,2%%DATE:~7,2%%TIME% %mysqldir%\mysqldump -u %mysqluser% -p%mysqlpassword% -h %mysqlhost% -P %mysqlport% --databases --routines --verbose gnucash_shockwave > %BackupDir%\gnucash_shockwave-%timestamp%.sql echo.------------------------------------------------------ echo. Backup complete! 

Any suggestions?

3

6 Answers

The Date and Time format depends on what you've specified in the Region and Language Control Panel applet.

Run the following batch file (which assumes dd/mm/yyyy and hh:mm:ss) and modify the substring extraction (using the : and ~ characters) as required to get the proper parts from both Date and Time strings:

@echo off cls echo Date format = %date% echo dd = %date:~0,2% echo mm = %date:~3,2% echo yyyy = %date:~6,4% echo. echo Time format = %time% echo hh = %time:~0,2% echo mm = %time:~3,2% echo ss = %time:~6,2% echo. echo Timestamp = %date:~6,4%-%date:~3,2%-%date:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2% 

For more help on substring extraction, type set /? at the command prompt or see this page.

6

Here's what worked for me.

Format - mmddyyyy_HHMMSS echo %DATE% %TIME% Date format = Thu 03/05/2015 15:48:26.22 echo mm = %date:~4,2% echo dd = %date:~7,2% echo yyyy = %date:~10,4% echo Timestamp = %date:~4,2%%date:~7,2%%date:~10,4%_%time:~0,2%%time:~3,2%%time:~6,2% Timestamp - 03052015_154013 
3

Sorry for the delay...

The only reliable way to get appropriate date whatever regional setting are is the solution of foxidrive @

@echo off for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%" set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%" echo datestamp: "%datestamp%" echo timestamp: "%timestamp%" echo fullstamp: "%fullstamp%" pause 
1

With a little tweaking I got this

@echo off cls echo Date format = %date% echo dd = %date:~0,2% echo mm = %date:~3,2% echo yyyy = %date:~6,8% echo. echo Time format = %time% echo hh = %time:~0,2% echo mm = %time:~3,2% echo ss = %time:~6,2% echo. echo Timestamp = %date:~0,2%_%date:~3,2%_%date:~6,8%-%time:~0,2%_%time:~3,2%_%time:~6,2% ECHO "TEST..." > test-%date:~0,2%_%date:~3,2%_%date:~6,8%-%time:~0,2%_%time:~3,2%_%time:~6,2%.txt 


Result:

"test-do_25_06-2015-22_21_51.txt" (Thursday_25th_June_2015-22:21.51)

2

i think this improves @Nick Rogers answer a little bit.

EX: with the german locate (de_DE) there is a leading whitespace at hours below 10.

also i wanted a leading zero at the hour. "echo - !^_hh! -" gives "09" instead of only "9"

so i break the code in two pieces for better reading. this example should match more locales.

+bonus: _ms = milliseconds or whatever the last two digits are (-;

remark: sometimes the HOUR needs to be escaped, see inside code

SETLOCAL SETLOCAL EnableExtensions SETLOCAL EnableDelayedExpansion @ECHO ON @rem use FOR /F to 'break out' the components of %DATE% and %TIME%, assuming 'yyyy/mm/dd' format date i.e. @rem works also with german standard format dd.mm.yyyy echo ----- date ----- echo _%date%_ for /F "tokens=1-3 delims=.-/" %%i IN ("%date%") DO Set "_Y=%%i"& Set "_M=%%j"& Set "_D=%%k" @rem Switch the year and day if appropriate IF "xx%_D:~2%" == "xx" Set "_Y=%_D%"& Set "_D=%_Y%" set __date=%_Y%%_M%%_D% echo _%__date%_ echo ----- date ----- echo ----- time ----- echo _%time%_ for /F "tokens=1-4 delims=:," %%l IN ("%time%") DO Set "_tmp_hh=%%l"& Set "_mm=%%m"& Set "_ss=%%n"& Set "_ms=%%o" @rem add leading zero to hour if necessary if %_tmp_hh% LSS 10 (set _hh=^0%_tmp_hh:~1,1%) ELSE (set _hh=%time:~0,2%) @rem sometimes the HOUR needs to be escaped, like this echo - !^_hh! - set __time=%_hh%%_mm%%_ss%%_ms% echo _%__time%_ echo ----- time ----- Set fullTime=%_Y%%_M%%_D%-%_hh%%_mm%%_ss%%_ms% echo _%fullTime%_ pause 

outputs: echo fullTime = 15062018-10182821

the shortest (most versatile?) answer is as per the following TimeStamp.cmd script containing just three lines of code (excluding comments etc.)...

@ECHO ON @REM use FOR /F to 'break out' the components of %DATE% and %TIME%, assuming 'yyyy/mm/dd' format date i.e. for /F "tokens=1-6* delims=.:-/ " %%i IN ("%DATE% %TIME%") DO Set "YYYY=%%i"& Set "MM=%%j"& Set "DD=%%k"& Set "HH=%%l"& Set "MI=%%m"& Set "SS=%%n" @REM Switch the year and day if appropriate IF NOT "X%DD:~2%" == "X" Set "YYYY=%DD%"& Set "DD=%YYYY%" Set "TimeStamp=%YYYY%%MM%%DD%%HH%%MI%%SS%" 

Note the need to use %% rather than % for the variable in a FOR statement in a batch file

...execution yields (blank lines and comments removed)...

C:\>timestamp.cmd C:\>for /F "tokens=1-6* delims=.:-/ " %i IN ("2018/03/02 11:17:43.35") DO Set "YYYY=%i" & Set "MM=%j" & Set "DD=%k" & Set "HH=%l" & Set "MI=%m" & Set "SS=%n" C:\>Set "YYYY=2018" & Set "MM=03" & Set "DD=02" & Set "HH=11" & Set "MI=17" & Set "SS=43" C:\>IF NOT "X" == "X" Set "YYYY=02" & Set "DD=2018" C:\>Set "TimeStamp=20180302111743" 
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy