Possible to create commands to compress to .zip a certain folder list? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Possible to create commands to compress to .zip a certain folder list?

20 Nov 2021, 12:00

Hi,

I'd appreciate your help.

For backup purposes, I intend to create zip files assembling several folders from different folders within my user directory.
For instance:
C:\Users\%username%\AppData\Roaming\Folder1
C:\Users\%username%\AppData\Roaming\Folder2
C:\Users\%username%\Documents\Folder1
C:\Users\%username%\Documents\Folder2

I know how to perform

Code: Select all

FileCopyDir
, and I'm wondering whether it is possible to run automatically:
1-FileCopyDir
2-Zip copied folders (I use 7-zip, so if it AHK could use it for zip it would be great)
3-Delete all copied folders, leaving only the .zip file

Can AHK perform such tasks?

Actually, it sounded that using directly with 7-zip command syntax (what I don't know how to do yet) could be simpler than using AHK.
But often AHK has so elegant solutions to everything that I'd appreciate to know how AHK would handle this scenario.

Thank you!
Carlos
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: Possible to create commands to compress to .zip a certain folder list?

20 Nov 2021, 13:00

See https://www.autohotkey.com/board/topic/60706-native-zip-and-unzip-xpvista7-ahk-l/ for the native approach to zipping files and folders.
No need to copy files or folders first.
Example:

Code: Select all

arr := ["C:\Users\" a_username "\scripts\browser\"
,"C:\Users\" a_username "\scripts\capture\"
,"C:\Users\" a_username "\scripts\gocr\"
,"C:\Users\" a_username "\scripts\hiedit\"]

stmp := "zip" . A_Year . A_MM . A_DD "_" A_Hour . A_min . A_sec . ".zip"
InputBox, sZip, , , , 450, 150, , , , , % a_scriptdir . "\" . stmp
if ErrorLevel
    return
Zip(arr,sZip)
return

Zip(FilesToZip,sZip,del=0)								; FilesToZip is an array, sZip is full path to new zipfile
	{
	cnt := FilesToZip.count()
	Progress, p1 CWFFFFFF CT000000 b w390 FS9, % "Zipping file 1 of " cnt  		; I know, deprecated	
	If Not FileExist(sZip)
		CreateZipFile(sZip)
	psh := ComObjCreate("Shell.Application")
	pzip := psh.Namespace(sZip)
	for x,y in FilesToZip
		{
		pzip.CopyHere(y, 1028)
		while (pzip.Items().count < x)
			sleep, 250
		Progress, % 100 * (x/cnt), % "Zipping file " pzip.Items().count + 1 . " of " . cnt   
		if (del = 1)
			FileDelete, %y% 
		}
	Progress, 100, Done!
	sleep, 1000
	Progress, off
	}
	
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()
	}
14.3 & 1.3.7
teadrinker
Posts: 4366
Joined: 29 Mar 2015, 09:41
Contact:

Re: Possible to create commands to compress to .zip a certain folder list?

20 Nov 2021, 19:07

flyingDman wrote:

Code: Select all

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()
	}
Weird code. I'd use

Code: Select all

CreateZipFile(sZip)
{
   File := FileOpen(sZip, "w")
   File.WriteInt(0x06054B50)
   File.Length := 22
}
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: Possible to create commands to compress to .zip a certain folder list?

20 Nov 2021, 20:30

Pretty old code. Anything that improves it is welcome! Thank you.
14.3 & 1.3.7
teadrinker
Posts: 4366
Joined: 29 Mar 2015, 09:41
Contact:

Re: Possible to create commands to compress to .zip a certain folder list?  Topic is solved

21 Nov 2021, 01:29

Perhaps, this is better:

Code: Select all

AddFilesToZip(FileArray, zipFilePath, UserFunc := "") {
   if !FileExist(zipFilePath) {
      File := FileOpen(zipFilePath, "w")
      File.WriteInt(0x06054B50)
      File.Length := 22
      File.Close()
   }
   ( !IsObject(FileArray) && FileArray := [FileArray] )
   Folder := ComObjCreate("Shell.Application").NameSpace(zipFilePath)
   count := Folder.Items.Count
   for k, file in FileArray {
      Folder.CopyHere(file)
      while Folder.Items.Count < count + k
         Sleep, 50
      ( UserFunc && %UserFunc%(file, FileArray.Count(), k) )
   }
}
However, this way is not quite reliable. You can't be sure, that the copy operation is complete. What if you add files to an existing archive and some files names already exist there?
RussF
Posts: 1292
Joined: 05 Aug 2021, 06:36

Re: Possible to create commands to compress to .zip a certain folder list?

21 Nov 2021, 07:38

With all due respect to all you great AHK coders, there is no need for a Rube Goldberg machine to perform such a simple task. 7-zip will use a list file (just a plain text file listing all the paths and files you want to archive) on its command line. Syntax here:
https://documentation.help/7-Zip/syntax.htm#:~:text=or%20a%20directory.-,List%20file,-You%20can%20supply

Then a simple

Code: Select all

Run, 7z a -tzip archive.zip @listfile.txt
is all the AHK code needed. (You may have to add a path to the 7z program, add additional arguments and/or play with quotes.)

If you want to add a new group of files to the archive, just edit the list file.

I subscribe to the KISS principle.

Russ

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, Bing [Bot], CoffeeChaton, DaviKar and 85 guests