1.

Solve : How to add date to folder in batch-file?

Answer»

I need a little bit help with batch-files: how can I rename a folder in a bat-file so that it CONTAINS a current date - such as folder1_20090108 (or any other date string format)? Without the need of typing the date myself, of course

I'm planning to create a simple batch file that would make a backup copy of one certain folder that contains my school project, and need that date to identify the version.

Cheers

JussiIt's always difficult when we do not know the format of your date which you didn't advise. Here is a rename if your date format is 'day mm/dd/yyyy' - not tested.

Ren folder1 folder1_%date:~-4%%date:~4,2%%date:~7,2%

Renamed folder should be in the format folder1_yyyymmdd

You can substitute Copy for Ren when you copy the folder, no sense in copying then renaming.

Hope this helps.Quote from: Dusty on January 09, 2010, 02:30:47 AM

It's always difficult when we do not know the format of your date which you didn't advise. Here is a rename if your date format is 'day mm/dd/yyyy' - not tested.

Ren folder1 folder1_%date:~-4%%date:~4,2%%date:~7,2%

Renamed folder should be in the format folder1_yyyymmdd

You can substitute Copy for Ren when you copy the folder, no sense in copying then renaming.

Hope this helps.

Thanks for the code. My mistake, I didn't explain the ORDER of numbers at example in my post - I did indeed mean format yyyymmdd.

-JussiDusty MEANS, you need to tell us the local date format you are using. You can see it using the date /t command. For example my local settings make the date look like this

Code: [Select]C:\>date /t
09/01/2010
Or use a vbs / batch hybrid which is locale - independent

Code: [Select]@echo off
Echo Wscript.echo eval(WScript.Arguments(0))>evaluate.vbs
For /f "delims=" %%Y in ( ' cscript //nologo evaluate.vbs "year(date)" ' ) do set yyyy=%%Y
For /f "delims=" %%M in ( ' cscript //nologo evaluate.vbs "month(date)" ' ) do set mm=%%M
For /f "delims=" %%D in ( ' cscript //nologo evaluate.vbs "day(date)" ' ) do set dd=%%D
del evaluate.vbs
if %mm% LSS 10 set mm=0%mm%
if %dd% LSS 10 set dd=0%dd%
set datestamp=%yyyy%%mm%%dd%
echo Datestamp is %datestamp%

Code: [Select]Datestamp is 20100109


Discussion

No Comment Found