We help IT Professionals succeed at work.

How to concatenate string inside batch file

jefcheek
jefcheek asked
on
24,083 Views
Last Modified: 2010-08-05
Inside the batch file, i need to pkzip a file. but the pkzip file name is to follow the current date so that i can keep track of the file. for eg

on 2/2/07 --  pkzip   file020207.zip     ppp
on 3/2/07 --  pkzip   file030207.zip     ppp
on 4/2/07 --  pkzip   file040207.zip     ppp

thus i want the zip file name to be dynamic follow according to the date.
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
Whooops, you only want the "07" for the year.
Change it to :
ren today.zip %date:~3,2%%date:~0,2%%date:~8,2%.zip

That works if your time is output in a Command window as 02/02/2007 (ie. dd/mm/yyyy) using the command   DATE  /T
The /T switch just outputs the date.  If you leave that switch off it prompts you to change the date.

Count the characters in  02/02/2007 from left to right and you'll see how it works for each of the variables:

%date:~0,2%  -  start at zero position and include next two characters (the 02 of dd)
%date:~3,2%  - start at the 3rd character of date (first slash) and include next two characters (02 of mm)
%date:~8,2%  - start at the 8th character of date (1st zero of yyyy) and include next 2 characters (07 of yyyy)

You'll be able to adapt the variables if your date is output as dd/mm/yy
%date:~3,2%%date:~0,2%%date:~6,2%

Author

Commented:
thanks, it's working. u r great
CERTIFIED EXPERT

Commented:
Thank you, Jeff.  Glad it worked.

Not sure if you noticed, but I got the dd and mm the wrong way around in the 3rd line of my 2nd comment.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.