23

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 Answers14

35

You can replace symbols in variables by using :

set _date=%DATE:/=-%
7

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
  • 33,097
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.

Putnik
  • 945
släcker
  • 566
3

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
  • 7,043
3

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
  • 498,455
3
zip some_options "archive_%date:/=-%.zip"

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

slhck
  • 235,242
Theo
  • 31
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
  • 63,447
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.

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
  • 63,447
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

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.

user
  • 30,336
0

%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%
03.04.2021

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
2021_04_03
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
-1

not sure if it suits you, but my code is

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