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.
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.
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:
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.
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).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
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.
Use the
%date%
and%time%
variables, and you can useFor /f
command to parse the tokens delimited by/
or locale-specific format to change formats.