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?

14 Answers 14


You can replace symbols in variables by using :

set _date=%DATE:/=-%
  • 1
    This is a neat trick! – Cristian 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) – Cristian 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
  • 8
    this is even faster than: %date:~-10,2%-%date:~-7,2%-%date:~-4,4% ! – Jess Stone Feb 7 '14 at 11:22
  • How has no one +1 your response Jess Stone! +1 from me!!! – Arvo Bowen Mar 29 '14 at 3:36

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).


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.


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.

  • 1
    Care to explain the monstrosity? :) – Cristian 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... – Cristian 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

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,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.

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%

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

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


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>
  • So it can do in-place substitutions! If you ask me, the 'for' command has one weird syntax... – Cristian 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

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

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


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.


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.

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
  • The backtick trick doesn't work in cmd.exe. And the Windows 'date' command can't handle formatting :( – Cristian Diaconescu Sep 28 '09 at 12:44

%date% and date /t command don’t work. I have different regional settings in different accounts and the following is output in them:

first account:

>echo %date%

second account:

>echo %date%
Sat 04/03/2021 

The solution that will probably suite me is:

REM file getCurrentDateFormatted.vbs

Function Rpad(strInput, length, character)
  Rpad = Right(String(length, character) & strInput, length)
end function

ts = Now()
WScript.StdOut.Write Year(ts) & "_" & Rpad(Month(ts), 2, "0") & "_" & Rpad(Day(ts), 2, "0")

Yes, there's no Rpad function in CSCRIPT :(

How to use:

>CSCRIPT /Nologo getCurrentDateFormatted.vbs

not sure if it suits you, but my code is

set ldt=%date% %time%
echo %ldt%>> logs.txt
  • The OP wants a file with the current date in the file name, not in the file contents – Jan Doggen Dec 2 '14 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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