-1
@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.
set timestamp=%date:~6,4%-%date:~3,2%-%date:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
F:\XAMPP\mysql\bin\mysqldump.exe -u root -pABC123 database >"C:\Users\Administrator\Desktop\backups\database_file_%timestamp%.sql"

The bat file breaks when I add the timestamp variable. I have tried a few different things but I am unsure why adding the timestamp variable would break it.

  • 1
    Fist two steps to debugging a batch file 1) Don't use `ECHO OFF`. 2) Run the batch file from a cmd prompt instead of using your mouse to execute it. This way you can see all of the verbose execution and the error messages it is generating. – Squashman Apr 12 '21 at 01:00
  • What does *breaks* mean specifically? – Ken White Apr 12 '21 at 01:00
  • Regardless of my previous comment it would be helpful to you to take the [tour] and read [ask] a good question. Also, please be certain you are providing a [mcve] of your code. – Squashman Apr 12 '21 at 01:03
  • 1
    I don't think this question should be tagged with "mysql". This question does not really have anything to do with mysql specifically – Craig Apr 12 '21 at 01:13
  • It would help us if you also told us what was being output for `%date%` and `%time%` on your PC, for the same user. – Compo Apr 12 '21 at 01:25

1 Answers1

0

Your problem seems to be that you're extracting the wrong characters in the date variable. The date value includes a three-character day (Mon 12/04/2021) which you're not skipping, so you're extracting the wrong values for yyyy, mm and dd.

Try this:

cls
echo Date format = %date%
echo dd = %date:~4,2%
echo mm = %date:~7,2%
echo yyyy = %date:~10,4%
echo.
echo Time format = %time%
echo hh = %time:~0,2%
echo mm = %time:~3,2%
echo ss = %time:~6,2%
echo.
set timestamp=%date:~10,2%-%date:~7,2%-%date:~4,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
F:\XAMPP\mysql\bin\mysqldump.exe -u root -pABC123 database >"C:\Users\Administrator\Desktop\backups\database_file_%timestamp%.sql"

Note that the date format is dependent on locale (and possibly Windows version). Most obviously the day and month will be interchanged between US English and UK English. Other languages will need to be adjusted for their specific formats.