Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch + regex + 7-zip

Posted on 2015-01-23
7
Medium Priority
?
308 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 25

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Accepted Solution

by:
NVIT earned 2000 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 25

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…
Suggested Courses

972 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