Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Unfortunately, this doesn't work on systems with the date formatted like y/m/d because of the slashes:

zip some_options "archive_%DATE%.zip"

The %DATE% variable expands to something like

Mon 09/28/2009

I have access to the gnuwin32 package, so I could use, say, sed to replace spaces and slashes by dashes. The problem is, how would I use the output of sed to create the file name of the zip archive?

In Unix (bash), one can use back-quotes to evaluate-in-place a command and use its output in another command, something like:

zip [...] archive_`echo %DATE% | sed -e s/.../.../`.zip

Is there anything similar available in Windows?

Or maybe there's a way to set a variable to the returned value of sed and use that to construct the file name?

share|improve this question

13 Answers 13

up vote 18 down vote accepted

You can replace symbols in variables by using :

set _date=%DATE:/=-%
share|improve this answer
    
This is a neat trick! –  Cristi Diaconescu Sep 28 '09 at 12:45
    
P.S. Is there a 'one-liner' way to get rid of spaces or other nasty characters too? (as opposed to having a 3rd...nth temp variable for each character to replace) –  Cristi Diaconescu Sep 28 '09 at 12:47
    
No sorry. There seems to be no such thing. You have to fallback to the for syntax to do that. NB: This is a nice reference for batch-programming : robvanderwoude.com/batchfiles.php –  Christopher Sep 28 '09 at 14:12
1  
this is even faster than: %date:~-10,2%-%date:~-7,2%-%date:~-4,4% ! –  Jess Stone Feb 7 at 11:22
    
How has no one +1 your response Jess Stone! +1 from me!!! –  Arvo Bowen Mar 29 at 3:36

This rather horrid bit of code should do something like what you want

move "Filename.txt" "Filename%date:~6,4%%date:~3,2%%date:~0,2%_%Time:~0,2%%Time:~3,2%%Time:~6,2%.txt"

If you change the filename appropriately.

share|improve this answer
1  
Care to explain the monstrosity? :) –  Cristi Diaconescu Sep 28 '09 at 12:43
    
It's basically chopping bits out of the date and feeding it to the middle of the filename, you can test it by doing echo then parts of the horror at the command line so if you do echo %date:~6,4% it gives the year. It's just substringing the date and time. –  Col Sep 28 '09 at 13:19
    
Interesting. Not too portable unfortunately... –  Cristi Diaconescu Sep 28 '09 at 14:27
    
Unfortunately the problem with using substrings is that it relies on a certain date format, and blows up when it's different. echo %date:~6,4%%date:~3,2%%date:~0,2% returns 9-289-20 on my machine since I have the date set to use yyyy-MM-dd (so today is 2009-09-28). –  Joshua Sep 28 '09 at 14:40
    
True, I only use this solution on one machine so it's not too much of an issue. –  Col Sep 28 '09 at 16:02
zip [...] archive_`echo %DATE% | tr / -`.zip

Should work.

On the other hand - perhaps you have date command available (I don't know Windows). If yes, then just do:

zip [...] archive_`date +'%Y-%m-%d'`.zip
share|improve this answer
    
The backtick trick doesn't work in cmd.exe. And the Windows 'date' command can't handle formatting :( –  Cristi Diaconescu Sep 28 '09 at 12:44

Since your question is tagged as Windows, I found this solution that works in the good old .bat files :
"Windows Batch File (.bat) to get current date in MMDDYYYY format",
and can probably be adapted to your case:

echo on
@REM Seamonkey’s quick date batch (MMDDYYYY format)
@REM Setups %date variable
@REM First parses month, day, and year into mm , dd, yyyy formats and then combines to be MMDDYYYY

FOR /F “TOKENS=1* DELIMS= ” %%A IN (’DATE/T’) DO SET CDATE=%%B
FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (’DATE/T’) DO SET mm=%%B
FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (’echo %CDATE%’) DO SET dd=%%B
FOR /F “TOKENS=2,3 DELIMS=/ ” %%A IN (’echo %CDATE%’) DO SET yyyy=%%Bv vSET date=%mm%%dd%%yyyy%

this does nothing but setup the %date variable to be todays date in MMDDYYYY format so it can be called later in the script.

Edit
Found a better syntax. Given that my date is today "Mon 28/09/2009":
set day=%Date:~0,2%
set month=%Date:~7,2%
set year=%Date:~10,4%

share|improve this answer

I have posted an answer to a very similar question here. The spltting of the date in fields is done in 1 line, instead of several lines in harrymc's answer.

The interesting part is:

@echo off
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd=%%k%%j%%i
echo Date: %yyyymmdd%

And the warning is still relevant:

Warning: the format of the date (yyyymmdd=%%k%%j%%i) depends on your regional settings. Because I use the French date format (dd/mm/yyyy), I have to use "%%k%%j%%i" as the format (%%i = day, %%j = month, %%j = year).

If your regional settings are set to US style (mm/dd/yyyy), you should use "%%k%%i%%j" (%%i = month, %%j = day, %%j = year).

share|improve this answer

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.

share|improve this answer

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
share|improve this answer

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>
share|improve this answer
    
So it can do in-place substitutions! If you ask me, the 'for' command has one weird syntax... –  Cristi Diaconescu Sep 28 '09 at 14:30
    
i didnt claim it could not :) the whole 'cmd' stuff is crazy anyway, whoever designed that was deep into some kind of bad dream. the 'for' cmd actually creates lines (aka 'unrolls' the 'loop') in the first pass and then executes the so 'created' script. –  akira Sep 28 '09 at 14:35

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

@echo data = %CDate%

This is in the Year-Month-Day that is most common now days

share|improve this answer

Any solution that uses either %DATE% or the output of date /t is likely to break if the user has modified his/her regional settings. For example, I've set my "short date" format to "yyyy-MM-dd", so I have %DATE% equal to "2012-02-18", and date /t produces the same output. (This is for Windows 7; I presume it's the same for earlier versions of Windows.)

Build a batch file and then execute it.

Since you have the GNU date command available, you can do something like this:

C:\path\to\gnu\date "+zip some_options archive_%Y-%m-%d.zip" > tmp.bat
call .\tmp.bat

tmp.bat will contain something like:

zip some_options archive_2012-02-18.zip

Note that this creates tmp.bat in the current directory, which might be a problem; it might be better to create %TEMP%\tmp.bat. I also didn't remove the file afterward; that's easy enough to do.

date +FORMAT lets you specify the output format of the date command, similar to a printf format string; see the coreutils date documentation for details. The format can include arbitrary text, so in this case you don't even need to use sed.

share|improve this answer
zip some_options "archive_%date:/=-%.zip"

the /=- substitutes the / with a minus sign.

share|improve this answer

not sure if it suits you, but my code is

@ECHO OFF
set ldt=%date% %time%
echo %ldt%>> logs.txt
share|improve this answer
    
The OP wants a file with the current date in the file name, not in the file contents –  Jan Doggen Dec 2 at 9:15

I guess, if you put everyone's bits together you get something that works for you. This is on a Windows 2003 system to give me a snapshot of running processes in response to a trigger from the performance monitor:

@echo off
For /f "tokens=1,2,3,4,5 delims=/. " %%i in ('date /t') do set CDate=%%k%%j%%i
For /f "tokens=1,2 delims=:" %%f in ('time /t') do set CTime=%%f%%g
tasklist /fo table >tasklist%CDate%%CTime%.txt

The output file is named in one string with no delimiters.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.