Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use 7Zip on each file within a directory, saving the resulting archives to a new directory
InputBox, password, Enter Password for Archives, The generated archives will be protected with the password you enter below. Your input will be masked., hide
; Using FileSelectFolder is just one way of choosing your folders.
FileSelectFolder, sourcepath,,, Source Folder
sourcepath := RegExReplace(sourcepath, "\\$") ; Removes the trailing backslash, if present.
FileSelectFolder, destinationpath,,, Destination Folder
destinationpath := RegExReplace(destinationpath, "\\$") ; Removes the trailing backslash, if present.
sourcelen := StrLen(sourcepath) + 1 ; Determine the start of the variable part of the path.
Loop, Files, %sourcepath%\*.*, R
{
varfilepath := SubStr(A_LoopFileFullPath, sourcelen) ; Grab everything to the right of the source folder.
RunWait, "c:\program files\7-zip\7z.exe" a "%destinationpath%%varfilepath%.7z" "%A_LoopFileFullPath%" -p"%password%" -t7z -mx0 -mhe -mmt,, Hide
FileCount := a_index
}
Msgbox Archives Created: %FileCount%`nSource: %sourcepath%`nDestination: %destinationpath%
@DaneWeber

This comment has been minimized.

Copy link
Owner Author

@DaneWeber DaneWeber commented Aug 20, 2015

The 7Zip flags are from the Super User question I was answering when I wrote this. The main value here is just as an example of the Loop, Run, StrLen, and SubStr commands. http://superuser.com/questions/959130/batch-file-to-create-7z-archive-for-each-file-in-folder-subfolder-to-a-new-de/959743

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment