|
Answer» Hi guys,
Trying to write a batch file for work which will zip the contents of a folder (.jpg snapshots from CCTV system) then delete the original if the zip exists then re name the zip with a Timestamp.
Managed to do all this using PKZIP and NAMEDATE, a small program I found on the net.. coding is...
cls c:\batch\pkzip -a 1.zip *.jpg IF EXIST 1.zip (del *.jpg)
c:\batch\namedate /O-1 /Y 1.zip
Not exaclty pretty but does the job. Is there anyway I cam include the PKZIP.exe and NAMEDATE in teh batch file to make it totally standalone and not RELIANT on them existing in c:\batch ?
If not is there another BETTER way to do it?
Thanks
PhilThere is no way of storing an exe in a batch file (without perhaps converting the binary to hex format, writing it out to a .hex file and using debug to recreate the original)
However you can use batch string slicing to convert %Date% into just digits and rename your zip file, getting rid of the need for namedate ... you will still have to refer to PkZip, but you could check it is there before running and stop with an appropriate error message if it doesnt.
GrahamThanks - can you point me to somewhere that explains splicing the batch date string?
PhilWithout an OS it's difficult to know if you have access to the %date% string. If you do, this may work:
Code: [Select]for /f "tokens=2-4 delims=/ " %%a in ("%date%") do set dt=%%c-%%a-%%b
You can use %dt% anywhere in your batch file as needed. You can ALSO scrunch the date without the hyphens by using set dt=%%c%%a%%b
Note: The format of the date is dependent on your local settings.
8-)Using command prompt through Win XP SP2... Regional settings are set to Spain.
CHeers!!!!If your date is day/mon/year then %%a=day, %%b=mon, %%c=year
If your date is year/mon/day then %%a=year, %%b=mon, %%c=day
Be sure to use the correct delimiter (/ or -)
There is a pattern to this madness! Once you've got the tokens, you can arrange them anyway you wish.
Good luck. 8-)
On XP, using a windows script would be much easier. You could get each discrete piece of the date directly, letting the system worry about the regional settings and the delimiters. Never used windows scripting... any pointers to it?
Thanks!
PhilOh one other thing... If i want to rename it to yesterday´s date?An easy way to get yesterday's date would be to subtract 1 from the current day. Of course nothing is that easy.....after all, what would plan on doing if today was say, Apr 1?
Batch language does not easily do date/time arithmetic. It can be done but you have to jump thru hoops to get there.
Check out the Script Center for date/time techniques and much more.
Good luck. 8-)Thanks for you help guys, read through the Windows Scripting site but think for this project will stick with the batch file.
As I have to do this batch in 4 different directories I´m trying to use the call command to run the batch files from one main batch file.
The subfolders contain the following batch file... ruben-ext1.bat..
cls c:\batch\kmra\pkzip -a Accts.zip *.jpg IF EXIST Accts.zip (del *.jpg)
c:\batch\kmra\namedate /O-1 /Y Accts.zip move *.zip "c:\batch\kmra\Accts\Archived"
and the main batch file is...
cls call c:\Batch\Kmra\Accts\ruben-ext1.bat
Now if I run ruben-ext1.bat it RUNS fine and as it should but if I run it from the main batch file I get an error from PKZip (E12) nothing to do!
Anyone help?
Cheers!
Anyone?? :-?If the master routine is in the parent folder, then calling the subfolder\subbatch leaves the current (ie working) directory as the parent, the subbatch needs to either cd to the working folder and cd to the parent after or always refer to the path (absolute or relative) where the files live
GrahamThanks graham, but not sure if thats my problem... reason being I changed the file in the second batch file to do another command other than pkzip and that worked fine...? Is there a known problem with PKZip and the Call command cos thats all I can think it is now....
Thanks!Where are the jpg files? Unless they are in the current directory, it's doubtful that pkzip can find them. As GPL mentioned, you need path names for excutable files unless they live in the current directory or they are on your PATH. Parameters (ie. *.jpg) also need paths if they do not exist in the current directory regardless if they exist in the PATH.
Paths can be fully qualified or relative but are needed so the OS can find all the pieces of your run unit.
Good luck. 8-)GOT IT!!!!! THANKYOU VERY MUCH!!!
Cheers guys, really appreciated!!!!!
|