Solved

Batch + regex + 7-zip

Posted on 2015-01-23
7
200 Views
Last Modified: 2015-01-26
Hi,

I am currently working on a batch script to compress files with 7-zip but I am having some difficulty with regular expressions.

I would like to compress any log file that ends with digits 0 to 365.

example :

data.log.001
data.log.002
data.log.365

Here is my batch file :

IF  EXIST C:\data\test.log.* (
"C:\Program Files\7-Zip\7z.exe" a -tzip "C:\data\archives\test.zip" "C:\data\test.log.*" -mm=lzma -mx9
) ELSE (

exit
)

The problem is that even if there is no log file, the batch file will still create an empty zip file in C:\data\archives
I tried several things like test.log.* test.log.??? test.log.[0-3][0-9][0-9] but nothing works.

To summarize, I just want compress existing log file that ends with digits 0 to 365 in C:\data. If there is no log file the batch file exit.

Any idea?

Thank you
0
Comment
Question by:spmt
  • 3
  • 3
7 Comments
 
LVL 14

Expert Comment

by:frankhelk
ID: 40566340
Hmmm - I personally hate such overcombined statements ... maybe you could try

IF NOT EXIST C:\data\test.log.* exit
"C:\Program Files\7-Zip\7z.exe" a -tzip "C:\data\archives\test.zip" "C:\data\test.log.*" -mm=lzma -mx9

Open in new window

0
 

Author Comment

by:spmt
ID: 40566413
Thank you for your reply.

It still creates an empty zip file when there is no log file in C:\data and in that way it still compress log file like test.log.04587545.

I just want it compress the log file ending with 3 digits (000 to 365)
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40566463
There's probably a better way than this but this should work...
forfiles /m test.log.??? /c "cmd /c echo @file>>Files.txt"

Open in new window

Pass Files.txt to 7-zip. I don't recall the specific option name. Look it up.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:spmt
ID: 40567097
In that way it does not recognize the 7-zip command.
0
 
LVL 23

Accepted Solution

by:
NVIT earned 500 total points
ID: 40567168
See the -i (Include filenames) switch in the 7-zip help.

Revise your .bat to something like:
forfiles /p C:\data /m test.log.??? /c "cmd /c echo @file>>Files.txt"
if exist Files.txt "C:\Program Files\7-Zip\7z.exe" a -tzip "C:\data\archives\test.zip" @Files.txt -mm=lzma -mx9

Open in new window

0
 

Author Comment

by:spmt
ID: 40571018
It works like that thank you very much!
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40571029
Thanks for the update, spmt! I'm glad it turned out well for you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now