Log in

View Full Version : DOS Batch Copy question



Kicker
01-18-2012, 05:27 PM
I have the need to create a quick batch file (DOS) that copies a file to a new directory. However, I need to "Add the date" to the filename when it is copied.

For example:
filename = TestFile.pdf
new filename = 1-19-12TestFile.pdf

The date needs to be the current system date. I know that %date% will give me the system date.

How do you combine the date and the filename.

Appreciate the help.

Crocus Crow
01-20-2012, 04:32 PM
Assuming echo %date% returns dd/mm/yyyy (swap the Day and Month variables if not), try something like this.

SET Today=%Date: =0%
SET Day=%Today:~-10,2%
SET Month=%Today:~-7,2%
SET Year=%Today:~-4%
COPY file.txt %Day%-%Month%-%Year%file.txt

See http://www.robvanderwoude.com/datetimentparse.php

Kicker
01-20-2012, 05:46 PM
Thank you. It has been many (way to many) years since I used DOS and batch files.

Appreciate the response.