Zip files in a script

If you have an application running, which creates a lot of log files – it would be neat to have simple functionality to zip those files. This is possible, just using dos scripts and the AT functionality.

First the script – it could look something like this:

 vb |  copy code |? 
FindDate ----------------------------------------------------
for /f "tokens=1-4 delims=/.- " %%A in ('date /t') do (
    set DD=%%A&set MM=%%B&set YYYY=%%C&set Junk=%%D&goto End)
 
:Parse --------------------------------------------------------------
echo Date: YYYY=%YYYY% MM=%MM% DD=%DD% JUNK=%Junk%
 
:End ----------------------------------------------------------------
move c:\log\*.log c:\log\*.bck
c:\tools\zip.exe -m c:\log\Log%YYYY%-%MM%-%DD%.zip c:\log\*.bck

Lets take a closer look.

First we get today’s date by getting date and splitting into the peaces YYYY, MM, DD, JUNK, where JUNK is seconds, milliseconds and so on.

 vb |  copy code |? 
for /f "tokens=1-4 delims=/.- " %%A in ('date /t') do (
set DD=%%A&set MM=%%B&set YYYY=%%C&set Junk=%%D&goto End)

Notice – the setting of DD, MM and so on, differs from system to system. The above line works for a Windows 2003, where as the following line would work for a Windows 2000:

 vb |  copy code |? 
set Junk=%%A&set DD=%%B&set MM=%%C&set YYYY=%%D&goto End)

Next we moves the files, this is to make sure that no application is writing to it.

 vb |  copy code |? 
move c:\log\*.log c:\log\*.bck

and finaly the files are being zipped, using a dos version of zip.

 vb |  copy code |? 
c:\tools\zip.exe -m c:\log\backup\Log%YYYY%-%MM%-%DD%.zip c:\log\*.bck

Now you have build the script, and can save into a file called backup.cmd (the extension could also be bat).

The next thing to do, is getting the script scheduled. In this way we don’t have to execute it manually every day. To do this we are using the AT command.

In a DOS prompt write:

 vb |  copy code |? 
AT 04:00 /every:M,T,W,TH,F,S,SU c:\tools\backup.cmd

Which means, that the script backup.cmd will be execute every day at 04:00. If you want to stop the scheduled job, simply perform an AT without parameters to get the ID and then afterwards make a AT ID /DELETE.

You can leave a response, or trackback from your own site.

Leave a Reply


× four = 16