Page 1 of 1

7-zip automation

Posted: Wed Nov 14, 2012 9:05 pm
by Usacomp2k3
Looking to do some compacting of data. Basically what I'm looking for is some lines of code I can throw into a batch file to compress some folders. The input would be the folder name (manually typed is fine). The result of running the script is that the folder would be compressed to a zip file and then the original folder deleted. The tricky part is that I need it to perform some sort of verification that all the contents of the folder were successfully put into the zip file prior to deletion. I don't see a flag for doing this in 1 step within the 7-zip command flags. I also am not sure what the best way to the verification.
I'm open to using another program to do thus, but in-place usage without installation is preferable.

Re: 7-zip automation

Posted: Wed Nov 14, 2012 9:12 pm
by CampinCarl
While I'm not particularly familiar with 7-zip's command line, I know it's possible as I did it ages ago. I can't find the batch files I wrote (were actually automated backup scripts), but this link is a good place to start. I know one of the command flags will let you do an update-and-verify type run.

Edit: After re-reading your post, I think you'll probably want to use the -l switch (which lists all the files in the archive), and then run a diff on that output to your original list.

Good luck! :)

Re: 7-zip automation

Posted: Thu Nov 15, 2012 3:30 pm
by Scrotos
I don't know enough about the FOR command, but...

dir | FIND "File(s)"
7z t test.zip | FIND "Files: "

C:\Program Files\7-Zip>dir | FIND "File(s)"
              17 File(s)      3,047,744 bytes
C:\Program Files\7-Zip>7z t test.zip | FIND "Files: "
Files: 17


I saw some snippet like thus:

FOR /F "tokens=2" %X IN ('7z t test.zip ^| FIND "Files: "') DO @SET COUNT=%X

I'm guessing it was able to pull the "17" from the 7zip results. You can probably manipulate that to pull the right "token" from the DIR command, too. Stuff the result into different variables, then a simple IF to throw an error if the numbers are different. BTW, that FOR statement works from the command line. I'm trying to integrate it into a .bat now.

I use 7zip to compress some SQL dumps on a daily process, handy:

"C:\Program Files\7-Zip\7z" -pSECRETPASSWORD u -tzip c:\temp\backup.zip c:\temp\daily\ -mx=9 -mfb=256 -mpass=5 -mem=AES256

I do see some issues with files that are really large, like over 1 GB. Sometimes 7zip will leave a .zip.tmp file and not finish creating the ZIP. I haven't cared enough to really delve into that; might be limited RAM on the machine and messing with the parameters would help. If I OCD enough about the FOR thing, I'll post any findings I have.

Re: 7-zip automation

Posted: Thu Nov 15, 2012 3:45 pm
by Scrotos
Ok, so from in a batch file:

@echo off
FOR /F "tokens=2" %%X IN ('7z t test.zip ^| FIND "Files: "') DO SET ZIPCOUNT=%%X
echo %zipcount%

FOR /F "tokens=1" %%X IN ('dir ^| FIND "File(s)"') DO SET DIRCOUNT=%%X
echo %dircount%

IF %zipcount% == %dircount% (
echo yay
) ELSE (
echo boo
)


Hope this is of value. In the IF, you could replace the == with EQU if you want. I'm unsure how that works with string comparisons as those are strings, not numbers. It probably makes no practical difference if there is any to begin with.

Re: 7-zip automation

Posted: Thu Nov 15, 2012 3:53 pm
by Scrotos
Last consideration. If you do it from a different directory, I am unsure if DIR will return an extra 2 "files" for . and .. in the listing. If so, adjust accordingly. Also, this does not take into account subdirectories or the contents thereof. If you are doing things more than one level deep, you've got more work cut out for you. Hopefully this gets you a starting point.

Ah, and since the method I posted uses 7zip's "test" command, you may be able to trap an ERRORLEVEL for another layer of checking.

Re: 7-zip automation

Posted: Thu Nov 15, 2012 4:05 pm
by Usacomp2k3
Apparently the latest beta has a -m command. Going to try it soon.

Re: 7-zip automation

Posted: Thu Nov 15, 2012 5:53 pm
by Scrotos
Huh?

http://sourceforge.net/projects/sevenzi ... ic/6063877

7-Zip 9.30 alpha Copyright (c) 1999-2012 Igor Pavlov 2012-10-26

Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...]
[<@listfiles...>]

<Commands>
a: Add files to archive
b: Benchmark
d: Delete files from archive
e: Extract files from archive (without using directory names)
h: Calculate hash values for files
l: List contents of archive
t: Test integrity of archive
u: Update files to archive
x: eXtract files with full paths
<Switches>
-ai[r[-|0]]{@listfile|!wildcard}: Include archives
-ax[r[-|0]]{@listfile|!wildcard}: eXclude archives
-bd: Disable percentage indicator
-i[r[-|0]]{@listfile|!wildcard}: Include filenames
-m{Parameters}: set compression Method
-o{Directory}: set Output directory
-p{Password}: set Password
-r[-|0]: Recurse subdirectories
-scs{UTF-8 | WIN | DOS}: set charset for list files
-sfx[{name}]: Create SFX archive
-si[{name}]: read data from stdin
-slt: show technical information for l (List) command
-so: write data to stdout
-ssc[-]: set sensitive case mode
-ssw: compress shared files
-t{Type}: Set type of archive
-u[-][p#][q#][r#][x#][y#][z#][!newArchiveName]: Update options
-v{Size}[b|k|m|g]: Create volumes
-w[{path}]: assign Work directory. Empty path means a temporary directory
-x[r[-|0]]]{@listfile|!wildcard}: eXclude filenames
-y: assume Yes on all queries

It's had -m for a while, see my sample command line for -mx=9, -mfb=256, -mpass=5, -mem-AES256. In the docs I see nothing that seems to be what you want. Unless that's documented in some of the beta/alpha release threads, he kinda spreads the new info around in each thread and I haven't followed many of them. If you find something, post back! :D

Re: 7-zip automation

Posted: Thu Nov 15, 2012 6:03 pm
by Usacomp2k3
An actual 'm' command, not a '-m' switch.

EDIT: Seems to work great. And apparently it's a branch beta, not in the main development...
http://sourceforge.net/projects/sevenzi ... ic/3957405

Re: 7-zip automation

Posted: Fri Nov 16, 2012 10:02 am
by Scrotos
A "move" command, huh? Yeah, that sounds like a better solution than cobbling together some batch scripts. Hopefully that feature one day makes it into the main program.