Zip Multiple Selected files with AHK Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
v1t3r
Posts: 2
Joined: 22 May 2022, 04:16

Zip Multiple Selected files with AHK

Post by v1t3r » 22 May 2022, 04:30

Hello! I am noob in AHK and I trying to make my own little backupper. Can't make(Literally don't know how) to make zipping of multiple selected files with FileSelectFile.
I used code from https://www.autohotkey.com/board/topic/60706-native-zip-and-unzip-xpvista7-ahk-l/
Here is my "code"

Code: Select all

/*           ,---,                                          ,--,    
           ,--.' |                                        ,--.'|    
           |  |  :                      .--.         ,--, |  | :    
  .--.--.  :  :  :                    .--,`|       ,'_ /| :  : '    
 /  /    ' :  |  |,--.  ,--.--.       |  |.   .--. |  | : |  ' |    
|  :  /`./ |  :  '   | /       \      '--`_ ,'_ /| :  . | '  | |    
|  :  ;_   |  |   /' :.--.  .-. |     ,--,'||  ' | |  . . |  | :    
 \  \    `.'  :  | | | \__\/: . .     |  | '|  | ' |  | | '  : |__  
  `----.   \  |  ' | : ," .--.; |     :  | |:  | : ;  ; | |  | '.'| 
 /  /`--'  /  :  :_:,'/  /  ,.  |   __|  : ''  :  `--'   \;  :    ; 
'--'.     /|  | ,'   ;  :   .'   \.'__/\_: |:  ,      .-./|  ,   /  
  `--'---' `--''     |  ,     .-./|   :    : `--`----'     ---`-'   
                      `--`---'     \   \  /                         
                                    `--`-'  
Zip/Unzip file(s)/folder(s)/wildcard pattern files
Requires: Autohotkey_L, Windows > XP
URL: http://www.autohotkey.com/forum/viewtopic.php?t=65401
Credits: Sean for original idea
*/

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;; --------- 	EXAMPLE CODE	-------------------------------------
; FilesToZip = C:\Users\V1t3r\Desktop\Hell Backkup\test  ;Example of folder to compress
; FilesToZip = D:\Projects\AHK\_Temp\Test\*.ahk  ;Example of wildcards to compress
; FilesToZip := A_ScriptFullPath   ;Example of file to compress
sZip := A_ScriptDir . "\Test.zip"  ;Zip file to be created
sUnz := A_ScriptDir . "\ext\"      ;Directory to unzip files

;
;Sleep, 500
;Unz(sZip,sUnz)
;; --------- 	END EXAMPLE 	-------------------------------------



;; ----------- 	THE FUNCTIONS   -------------------------------------
Zip(FilesToZip,sZip)
{
If Not FileExist(sZip)
	CreateZipFile(sZip)
psh := ComObjCreate( "Shell.Application" )
pzip := psh.Namespace( sZip )
if InStr(FileExist(FilesToZip), "D")
	FilesToZip .= SubStr(FilesToZip,0)="\" ? "*.*" : "\*.*"
loop,%FilesToZip%,1
{
	zipped++
	ToolTip Zipping %A_LoopFileName% ..
	pzip.CopyHere( A_LoopFileLongPath, 4|16 )
	Loop
	{
		done := pzip.items().count
		if done = %zipped%
			break
	}
	done := -1
}
ToolTip
}

CreateZipFile(sZip)
{
	Header1 := "PK" . Chr(5) . Chr(6)
	VarSetCapacity(Header2, 18, 0)
	file := FileOpen(sZip,"w")
	file.Write(Header1)
	file.RawWrite(Header2,18)
	file.close()
}

Unz(sZip, sUnz)
{
    fso := ComObjCreate("Scripting.FileSystemObject")
    If Not fso.FolderExists(sUnz)  ;http://www.autohotkey.com/forum/viewtopic.php?p=402574
       fso.CreateFolder(sUnz)
    psh  := ComObjCreate("Shell.Application")
    zippedItems := psh.Namespace( sZip ).items().count
    psh.Namespace( sUnz ).CopyHere( psh.Namespace( sZip ).items, 4|16 )
    Loop {
        sleep 50
        unzippedItems := psh.Namespace( sUnz ).items().count
        ToolTip Unzipping in progress..
        IfEqual,zippedItems,%unzippedItems%
            break
    }
    ToolTip
}
;; ----------- 	END FUNCTIONS   -------------------------------------


;--------- GUI ----------
Gui, Add, Tab3,w330 h85,Zip Files|Folder Zip|Folder Unzip
Gui, Tab, Folder Zip
Gui, Add, Edit,r1 vFolderToZip w200, %FolderToZip%
Gui, Add , Button,x230 y33 w102, FolderSelect
Gui, Add , Button,x21 y60 w312, ZipIt
Gui, Tab, Zip Files
Gui, Add, Edit,r1 v1FilesToZip w200, %1FilesToZip%
Gui, Add , Button,x230 y33 w102, FilesSelect
Gui, Add , Button,x21 y60 w312, ZipItFile
Gui, Tab, Folder Unzip
Gui, Add, Edit,r1 vFileToUnzip w200, %FileToUnzip%
Gui, Add, Edit,r1 vWhereToUnzip w200, %WhereToUnzip%
Gui, Add , Button, x230 y33, SelectZip
Gui, Add , Button, x290 y33, UnzipIt
Gui, Add , Button, x230 y60 w102, ExtractFolder
Gui, Show
Return

ButtonFilesSelect:
FileSelectFile, 1FilesToZip, M3
GuiControl,, 1FilesToZip, %1FilesToZip%
Return

ButtonZipItFile:
Zip(1FilesToZip,sZip)
Return

ButtonFolderSelect:
FileSelectFolder, FolderToZip
GuiControl,, FolderToZip, %FolderToZip%
Return

ButtonZipIt:
Zip(FolderToZip,sZip)
;Zip(FilesToZip,sZip)
MsgBox, Done
Return

ButtonSelectZip:
FileSelectFile, FileToUnzip, , ,Select Zip,Zip Files (*.zip)
GuiControl,,FileToUnzip, %FileToUnzip%
Return

ButtonExtractFolder:
FileSelectFolder, WhereToUnzip
GuiControl,,WhereToUnzip, %WhereToUnzip%
Return

ButtonUnzipIt:
Unz(FileToUnzip,WhereToUnzip)
MsgBox, Done
Return

User avatar
mikeyww
Posts: 26889
Joined: 09 Sep 2014, 18:38

Re: Zip Multiple Selected files with AHK  Topic is solved

Post by mikeyww » 22 May 2022, 05:10

Welcome to this AutoHotkey forum!

In this case, it will help to dive into the script to understand it.

Code: Select all

Zip(FilesToZip,sZip) {
 (!FileExist(sZip)) && CreateZipFile(sZip)
 psh := ComObjCreate("Shell.Application"), pzip := psh.Namespace(sZip)
 For fileNum, file in StrSplit(FilesToZip, "`n")
  If (fileNum > 1) {
   zipped++
   ToolTip, Zipping %file%
   pzip.CopyHere(dir "\" file, 4|16)
   Loop
    If (zipped = done := pzip.items().Count)
     Break
   done := -1
  } Else dir := file
 ToolTip
}

v1t3r
Posts: 2
Joined: 22 May 2022, 04:16

Re: Zip Multiple Selected files with AHK

Post by v1t3r » 22 May 2022, 06:00

Thanks! It works perfect.

lsramos
Posts: 3
Joined: 29 Jul 2022, 08:23

Re: Zip Multiple Selected files with AHK

Post by lsramos » 03 Oct 2022, 17:57

Thanks for sharing the code. Better than the powershell method I've been using. I think.

I am zipping several hundred files at once. Most attempts succeed but every now and then it fails with "File not found or no read permission". My testing has been on the same folder of files and the failures occur after a random number of files were zipped--never fails at the same place.

User avatar
mikeyww
Posts: 26889
Joined: 09 Sep 2014, 18:38

Re: Zip Multiple Selected files with AHK

Post by mikeyww » 03 Oct 2022, 20:12

I do not have an answer, but you might have a look at the recent post by lexikos about Zip files.

viewtopic.php?f=76&t=109016

Post Reply

Return to “Ask for Help (v1)”