Solved

Batch + regex + 7-zip

Posted on 2015-01-23
7
218 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 24

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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

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

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 24

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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,…
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…

829 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