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?
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.6601This 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.9406this is even faster than:
%date:~-10,2%-%date:~-7,2%-%date:~-4,4%
! – Jess Stone – 2014-02-07T11:22:17.970How has no one +1 your response Jess Stone! +1 from me!!! – Arvo Bowen – 2014-03-29T03:36:05.160