Home » Questions » Computers [ Ask a new question ]

Windows command-line: create a file with the current date in its name

Windows command-line: create a file with the current date in its name

I'm trying to create a zip file from a batch script. I need it to be named like archive_.zip

Asked by: Guest | Views: 351
Total answers/comments: 5
Guest [Entry]

"You can replace symbols in variables by using :

set _date=%DATE:/=-%"
Guest [Entry]

"I always use:

For /f ""tokens=1,2,3,4,5 delims=/. "" %%a in ('date/T') do set CDate=%%a%%b%%c%%d

Then CDate will be Sat02182012. If you want to make it more sortable set CDate to %%d-%%b-%%c so it will be 2012-02-18

For /f ""tokens=1,2 delims=:"" %%f in ('time /t') do set CTime=%%f%%g

To make date and time folder/file friendly."
Guest [Entry]

"zip some_options ""archive_%date:/=-%.zip""

the /=- substitutes the / with a minus sign."
Guest [Entry]

"pure cmd.exe version in combination with the gnuwin32-'date':

%> for /F ""usebackq"" %d in ( `date.exe +""%y%m%d""` ) do zip archive_%d.zip <folder>"
Guest [Entry]

"what about using cscript + vbasic:

WScript.shell.run ""zip archive_"" & DatePart(""yyyy"", Now) & ""_"" & DatePart(""m"", Now) & ""_"" & DatePart(""d"", Now) & "".zip "" & WScript.arguments(0)

call it via

cscript /nologo zip_it.vbs"