Batch File Help Need: Zip all files before the current date.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lotek
Posts: 1
Joined: 18 Feb 2009 10:33

Batch File Help Need: Zip all files before the current date.

#1 Post by lotek » 18 Feb 2009 10:43

I'm new to the forum and I need help trying to create a batch file to do the following:

I have a bunch of log files that I need to zip and I would like to automate the process.

The filenames are in the format LOGMMDDYYYY.csv - where the MMDDYYYY portion is a particular date that the log file was created. The problem is that the log for the current day will be locked because it is in use by the program; therefore, I would like to create a batch file that would take all the logs before the current date and zip them and then move them to another folder.

Is this possible to do with a batch file?

Thanks for any help you may be able to give.

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 23 Feb 2009 10:51

Based on your system date (%Date%) being something like "02/23/2009", you could start with something like:

Code: Select all

Set "WD=%Date%"
Set "dMM=%WD:~0,2%"
Set "dDD=%WD:~3,2%"
Set "dYY=%WD:~6,4%"
Set "dYY2=%WD:~8,2%"

Set "dMMDDYYYY=%dMM%%dDD%%dYY%"
Set "TodaysLog=LOG%dMMDDYYYY%.csv"

For /F "Delims="  %%F in ('Dir Log*.csv') do (

   If /I [%%F] NEQ [%TodaysLog%] Zip -a %%F ZipFile

)



Modify the "Zip" line to your specifications and zip program.

It's crude, but it's a start...

-Rick

_m
Posts: 6
Joined: 27 Jan 2009 10:56

#3 Post by _m » 23 Feb 2009 14:34

You could use:

Code: Select all

FOR /F "DELIMS=" %%$ IN ('DIR LOG*.CSV') DO (
IF /I [%%$] NEQ [LOG%DATE:/=%.CSV] ZIP -a %%$ ZIPFILE
)


Instead of:

Code: Select all

Set "WD=%Date%"
Set "dMM=%WD:~0,2%"
Set "dDD=%WD:~3,2%"
Set "dYY=%WD:~6,4%"
Set "dYY2=%WD:~8,2%"

Set "dMMDDYYYY=%dMM%%dDD%%dYY%"
Set "TodaysLog=LOG%dMMDDYYYY%.csv"

For /F "Delims="  %%F in ('Dir Log*.csv') do (

   If /I [%%F] NEQ [%TodaysLog%] Zip -a %%F ZipFile

)

Post Reply