driftandwander
driftandwander

Reputation: 39

Batch script to archive subfolder contents using 7zip with FOR /R

I have a folder structure like this:

C:\\\Logs\logs1\tracelogXXXX.log  
C:\\\Logs\logs2\tracelogXXXX.log  
C:\\\Logs\logs3\tracelogXXXX.log

Each folder has a bunch of tracelogXXXX's, and I've got the pseudocode for a script that loops through each folder, archives each log into its own .zip, and then delete the tracelog left outside the archive (because 7zip doesnt have move functionality).

But I have no batch experience really, and I can't even get the zipping to work properly.

I can't access the documentation for 7zip from where I am currently, so I've tried this:

CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip"

And also this:

CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip" "%%i\"

The first one goes and zips all of \Logs for each instance of a .log file, making many zips each bigger than the last. And the second makes zips for each instanceof a .log file, with nothing in them.

How do I just zip each log file, in its own zip, named after itself, while operating from the parent directory? Deleting the outer files afterwards doesn't seem hard to accomplish once I figure out whats wrong with this syntax, but this is the important part!

Upvotes: 1

Views: 1148

Answers (1)

Jason Faulkner
Jason Faulkner

Reputation: 6568

You can do this from the command line with no batch file needed:

FOR /F "usebackq tokens=* delims=" %A IN (`DIR "C:\Logs\*.log" /B /S`) DO "C:\Path\To\7za.exe" a "%~dpnA.zip" "%~fA" & DEL "%~fA"

To use in a batch file, just replace each % with %%.

Upvotes: 0

Related Questions