Solved

Batch + regex + 7-zip

Posted on 2015-01-23
7
192 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 13

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

757 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

20 Experts available now in Live!

Get 1:1 Help Now