Solved

Batch + regex + 7-zip

Posted on 2015-01-23
7
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 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 25

Expert Comment

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

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
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…
Suggested Courses

617 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