1.

Solve : Inserting today's date in a filename via a batch file?

Answer»

I've created a simple batch file that copies a DOZEN small files from multiple directories into a single directory and then compresses them into a zip file.  I have to do this each day, then ftp the zip file to a co-worker in another facility. 

The first PART of the file name will always be the same - let's call it zipfile.zip - but I need to append the current date to the end of the filename in a YYYYMMDD format to keep the filenames unique.

Is there a way, within a batch file,  to extract the current date, convert it to yyyymmdd format, and then rename zipfile.zip to zipfileyyyymmdd.zip? 

Thanks in advance...
 What is your local date format, i.e. what do you see (copy and paste it) when you type

echo %date% (press enter)

at the prompt?

Wed 03/05/2008 Quote from: NyteOwl on MARCH 05, 2008, 10:36:14 AM

Wed 03/05/2008

OK so it's US date format

Quote
Is there a way, within a batch file,  to extract the current date, convert it to yyyymmdd format, and then rename zipfile.zip to zipfileyyyymmdd.zip?

set today=%date:~10,4%%date:~7,2%%date:~4,2%
ren zipfile.zip zipfile%today%.zip


 
I plugged this into my existing batch file, and it worked like a charm.

Thank you so much for the assist!!

Quote from: Dias de verano on March 05, 2008, 11:55:26 AM
set today=%date:~10,4%%date:~7,2%%date:~4,2%
ren zipfile.zip zipfile%today%.zip


Just noticed this morning that the above syntax re-formats the date as YYYYDDMM as opposed to yyyymmdd.  Simple fix, though.  Thanks again! You're right; I guess I missed it because I'm used to the European dd-mm-yyyy format. But you overcame that.


Discussion

No Comment Found