how to set date to current date using dos batch file command.
If you need to use the current date in a batch file, the variable %date% has the current date:
echo %date%
23/02/2010
It uses the format of the regional setting of your computer. In my computer it's dd/mm/yyyy.
Since the / can't be part of a file name, they must be replaced with a safe character or nothing:
echo %date:/=-%
23-02-2010
echo %date:/=%
23022010
If you want to create a backup copy of a file, you can do something like:
copy file.txt file-%date:/=%.txt
dir /b file*.*
file-23022010.txt
file.txt
Or first set it to a variable and then use it:
set currdate=%date:/=%
copy file.txt file-%currdate%.txt
The date command is what you are looking for. This works on my Windows XP box:
date 15-02-2010
Notice the formatting dd-MM-yyyy, which seems to be required here, probably because of my regional settings being set to Denmark. The documentation states that the format is MM-dd-yy, but on my computer, the day and month fields gets flipped if the date is written in that format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With