I have scheduled backup script that makes the database dump. How can I add the date timestamp to the file name?

I am talking about Windows and CMD.

Bogdan Gusiev
  • 593
  • 2
  • 6
  • 9

5 Answers5


In the command prompt and batch files, you can use %date% and %time% to return the date and time respectively. Date works fine, but the time value returned contains colons, which are illegal for use in filenames, but there is a way to remove those.

Use something like:

COPY file.txt file_%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt

This will produce a filename such as file_172215_01062009.txt

Update: The comments below have interesting twists on this command as well as some potential problems you can avoid.

  • 456
  • 3
  • 6
  • 4
    Great little tip. BTW, if you do it the other way around, that is: "file_20090601_172215", you will see the files in the correct order in an alphabetic list. –  Jun 01 '09 at 13:00
  • Yeah - that's a good tip too! – aalaap Jun 01 '09 at 13:12
  • 6
    You can also use %time::=% to remove the colons. – user1686 Jun 01 '09 at 13:33
  • This is getting so much more interesting! I remember how crazy I used to go making batch files as a kid. I feel like that all over again!!! Go batch go! – aalaap Jun 01 '09 at 15:21
  • 5
    I'd say that date substring-ing would break as soon as the system locale differs from when the script was tested? It could even work for the testing user and break as the script runner because of different date display formats set... – Oskar Duveborn Jun 01 '09 at 18:17
  • 5
    Beware hours 0 - 9! CMD sets the first character of the hour to , rather than 0. The COPY command above is going to have problems if you don't enclose the destination filename in quotes. – Evan Anderson Jun 01 '09 at 20:09
  • Excellent info about the hour character, Even, and yeah, Oskar, I didn't think about the locale, but it's important to consider that too! – aalaap Jun 02 '09 at 02:07
  • 5
    Note that `%DATE%` and `%TIME%` are locale-aware! It means that on a European machine, you get `DD.MM.YYYY`. Arrgh. – Pekka Jan 26 '11 at 20:36

Use the %DATE% and/or %TIME environment variables, optionally substituting the characters that are not allowed in filenames, using %name:from=to% (%TIME::=% would remove all colons).

theory » echo %date%

theory » echo %time%

theory » echo %time::=%

theory » echo %time::=,%

theory » echo backup-%date%-%time::=-%.zip
  • 10,162
  • 1
  • 26
  • 42
  • 3
    wow, never know about the :x=y trick. that's really cool. – Eugene Katz Jun 01 '09 at 21:37
  • 1
    +1 for the := trick, iv'e been in this game since MS-Dos 1 and that's the first time Iv'e seen that one. Would be even better if you could change multiple chars too!! :-) – shawty Sep 17 '18 at 12:02
  • Well, it was only added to Cmd.exe in the Windows NT series... And if you want to perform multiple changes, just have a chain of `set tmp=%tmp:x=y%` etc. – user1686 Sep 17 '18 at 12:12
  • 3
    The content of %DATE% (number of fileds, content and ordering) are locale specific. – symcbean Apr 04 '19 at 13:26

The only reliable way to get appropriate date whatever regional setting are, is the solution from foxidrive @ https://stackoverflow.com/questions/11037831/filename-timestamp-in-windows-cmd-batch-script

@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%"
Eric Ouellet
  • 303
  • 3
  • 9

Use %DATE% variable in the filename.

There is a %TIME% variable as well, but it contains characters not allowed in a file name.

Here is an example of writing a line of text into a new file, where the file created has a date and time in its name.

echo "testfile" >> backup-%DATE%.txt
Frode Lillerud
  • 1,656
  • 3
  • 18
  • 20

Use the %date% and %time% variables, and you can use For /f command to parse the tokens delimited by / or locale-specific format to change formats.

  • 321
  • 2
  • 8