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

19

4

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?

Cristi Diaconescu

Posted 2009-09-28T11:31:40.347

Reputation: 2 810

Answers

27

You can replace symbols in variables by using :

set _date=%DATE:/=-%

Christopher

Posted 2009-09-28T11:31:40.347

Reputation: 909

You can remove a space in a variable using %date: =% (space on the left, nothing on the right) – uSlackr – 2014-12-05T15:31:29.933

@JessStone I do prefer your style (thus the +1) but it assumes the system DATE format (for this user) is "Ddd YYYY/MM/DD" and makes the result "DDMMYYYY" but what if the system's DATE format is: "Ddd MM/DD/YYYY" then it becomes "MYYYDDMM" and cause no end of confusion. So I prefer to test several common formats, like this: if "%DATE:~6,1%"=="/" set D=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2% and similar for other formats. – Jesse Chisholm – 2015-07-21T16:59:24.660

1This is a neat trick! – Cristi Diaconescu – 2009-09-28T12:45:09.613

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 – 2009-09-28T12:47:00.423

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 : http://www.robvanderwoude.com/batchfiles.php

– Christopher – 2009-09-28T14:12:20.940

6this is even faster than: %date:~-10,2%-%date:~-7,2%-%date:~-4,4% ! – Jess Stone – 2014-02-07T11:22:17.970

How has no one +1 your response Jess Stone! +1 from me!!! – Arvo Bowen – 2014-03-29T03:36:05.160

5

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.

släcker

Posted 2009-09-28T11:31:40.347

Reputation: 546

4

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

Snark

Posted 2009-09-28T11:31:40.347

Reputation: 30 147

3

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

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

Theo

Posted 2009-09-28T11:31:40.347

Reputation: 31

2

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.

Col

Posted 2009-09-28T11:31:40.347

Reputation: 6 995

1Care to explain the monstrosity? :) – Cristi Diaconescu – 2009-09-28T12:43:58.477

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 – 2009-09-28T13:19:06.430

Interesting. Not too portable unfortunately... – Cristi Diaconescu – 2009-09-28T14:27:57.357

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 – 2009-09-28T14:40:02.517

True, I only use this solution on one machine so it's not too much of an issue. – Col – 2009-09-28T16:02:13.193

2

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%

harrymc

Posted 2009-09-28T11:31:40.347

Reputation: 306 093

2

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>

akira

Posted 2009-09-28T11:31:40.347

Reputation: 52 754

So it can do in-place substitutions! If you ask me, the 'for' command has one weird syntax... – Cristi Diaconescu – 2009-09-28T14:30:43.933

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 – 2009-09-28T14:35:06.283

1

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.

Keith Thompson

Posted 2009-09-28T11:31:40.347

Reputation: 4 645

1

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

akira

Posted 2009-09-28T11:31:40.347

Reputation: 52 754

1

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

Buffme

Posted 2009-09-28T11:31:40.347

Reputation:

1

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.

Anton Liebscher

Posted 2009-09-28T11:31:40.347

Reputation: 11

0

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

user7385

Posted 2009-09-28T11:31:40.347

Reputation:

The backtick trick doesn't work in cmd.exe. And the Windows 'date' command can't handle formatting :( – Cristi Diaconescu – 2009-09-28T12:44:31.847

-1

not sure if it suits you, but my code is

@ECHO OFF
set ldt=%date% %time%
echo %ldt%>> logs.txt

Kelly

Posted 2009-09-28T11:31:40.347

Reputation: 1

The OP wants a file with the current date in the file name, not in the file contents – Jan Doggen – 2014-12-02T09:15:28.407