Example of using AutoHotKey instead of a batch file

On Super User I addressed a question about using a batch file to create a 7z file (like a ZIP file) of each individual file within a directory. That’s painful when you’re restricted to a Windows batch file, but once the questioner mentioned that AutoHotKey was an option, I thought:

PROBLEM SOLVED

AHK’s StrLen and SubStr can extract the variable portion of the file path. The file loop will recurse through all the files in the source directory. And then it’s just a matter of using RunWait to pass the paths to 7-Zip. The “,, Hide” specified at the end of the RunWait tells it to hide the command windows spawned.

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%

I added in some pretty easy (in AHK) UI elements to enter a password, select the source and destination directories, and to be notified when the operation finished.

Note that you need v1.1.21+ of AHK or above for the file loop to operate as written.

Enter a password
Enter a password
Folder Selection
Folder Selection
Completion Summary
Completion Summary
Example of using AutoHotKey instead of a batch file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s