Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
I've reassociated your accounts, you should be able to log in and comment on your own posts and answers to your questions now. – slhck Nov 29 '12 at 21:18
    
slhck, lol thanks i was wondering what was going on then I remembered I wasn't at stack overflow lol – guyfromfl Nov 29 '12 at 21:36
    
replacing %date% by %date:~6,4%-%date:~3,2%-%date:~0,2% worked – Enora Nedelec Nov 27 '15 at 10:05
up vote 11 down vote accepted

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.

share|improve this answer
    
Thanks, I think I understand how this is working now. I am not at the office, but will try that as soon as I get there and post back. We are using Windows 8 Thanks for the help – guyfromfl Nov 29 '12 at 20:51
    
Got it, Had to tweak the string locations of the date, probably because of my location and language but that did the trick! Thank you very much! – guyfromfl Nov 30 '12 at 3:29

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
share|improve this answer
2  
Welcome to Super User! Could you please edit your answer to include some explanation of what your commands do? – Excellll Mar 5 '15 at 22:20
    
You’re answering the wrong question.  The OP’s question asks for YYYYMMDD; this answer gives MMDDYYYY. – Scott Jul 4 '15 at 8:53

With a little tweeking 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)

share|improve this answer
    
How does this differ from the other answers? – suspectus Jun 25 '15 at 20:48
    
@suspectus: This answer differs from abbs’s answer in that abbs’s assumes a U.S. Region (i.e., locale), where %DATE% ~ Ddd MM/DD/YYYY, whereas this one assumes a non-U.S. Region, where %DATE% ~ DD/MM/YYYY (without the day of the week).  And this one differs from Karan’s answer in that this one is wrong — the question asks for YYYYMMDD; this answer gives DD_MM–YYYY. – Scott Jul 4 '15 at 8:54
    
Thank you for the clarification Scott. – suspectus Jul 4 '15 at 18:20

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.