How to Extract all Zipped Files In a Directory While Preserving Folder Structure

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AlFlo
Posts: 359
Joined: 29 Nov 2021, 21:46

How to Extract all Zipped Files In a Directory While Preserving Folder Structure

09 May 2022, 17:17

I want to extract all zipped files within a directory, and have the file/subfolder/folder/directory structure preserved in Windows Explorer.

Shadowpheonix wrote a script which is pretty close: viewtopic.php?f=76&t=9240&p=461352#p461352 . But it seems to delete the actual zipped folder name, as I explain here: viewtopic.php?p=461352#p461352

Any ideas?
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

09 May 2022, 18:39

The native unz() function preserves the folder structure and does not delete the zip file.

Code: Select all

Unz(sZip, sUnz)									; zip file, folder to unzip to
	{
	FileCreateDir, %sUnz%
    psh  := ComObjCreate("Shell.Application")
    psh.Namespace( sUnz ).CopyHere( psh.Namespace( sZip ).items, 4|16 )
	}
example:

Code: Select all

zipfile := "C:\Users\" A_UserName "\Scripts\zip_test.zip"
unz(zipfile,A_Temp)
return
originally posted here: https://www.autohotkey.com/board/topic/60706-native-zip-and-unzip-xpvista7-ahk-l/
14.3 & 1.3.7
AlFlo
Posts: 359
Joined: 29 Nov 2021, 21:46

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

09 May 2022, 19:26

flyingDman, that looks great!

I assume I have to create a loop function to go through all of the files in the directory? If so, would the script be something like this:

Code: Select all

zipfile := c:\users\REST OF PATH OF THE DIRECTORY

loop, Files, zipfile, FDR
Unz(zipfile,A_Temp)
return
??
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

09 May 2022, 20:40

Something like this:

Code: Select all

zipfolder := "C:\Users\" A_UserName "\Scripts"      ; path of folder containing the zip files
dstntnfldr := A_temp						        ; path of the destination folder	
loop, files, %zipfolder%\*.zip, FR
	Unz(A_LoopFileFullPath, dstntnfldr)
14.3 & 1.3.7
AlFlo
Posts: 359
Joined: 29 Nov 2021, 21:46

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

10 May 2022, 12:11

flyingDman wrote:
09 May 2022, 20:40
Something like this:

Code: Select all

zipfolder := "C:\Users\" A_UserName "\Scripts"      ; path of folder containing the zip files
dstntnfldr := A_temp						        ; path of the destination folder	
loop, files, %zipfolder%\*.zip, FR
	Unz(A_LoopFileFullPath, dstntnfldr)
Hmmm, I think I'm doing something wrong. Nothing gets created to my dstntnfldr ...
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

10 May 2022, 13:56

Are you looking in the correct temp directory?

C:\Users\<UserName>\AppData\Local\Temp

You can add MsgBox % A_temp to your script and it will display your temp location.
User avatar
tank
Posts: 3130
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

10 May 2022, 14:28

run as admin?
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

10 May 2022, 14:39

I am using this function on a very regular basis to unzip downloads from sharepoint. I do not need admin privileges to do that.
14.3 & 1.3.7
AlFlo
Posts: 359
Joined: 29 Nov 2021, 21:46

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

10 May 2022, 15:34

I think my problem is either:

(1) I'm not sure how to use/access the temp folder; or

(2) I haven't added code to the script to be usable.

Specifically,

Code: Select all

MsgBox % A_temp
confirms the correct temp folder. But when I run the script, the temp folder solely shows recent files with 0 bytes.

This doesn't seem helpful. What am I doing wrong?

Or do I need to add some code, and flyingDman is just giving me one portion of the code I need?
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

10 May 2022, 17:09

This is the only code you'll need:

Code: Select all

zipfile := "C:\Users\" A_UserName "\Scripts\zip_test.zip"	; change to your needs
unz(zipfile,A_Temp)								; can be another folder, change to your needs	
return

Unz(sZip, sUnz)									; zip file, folder to unzip to
	{
	FileCreateDir, %sUnz%
    psh  := ComObjCreate("Shell.Application")
    psh.Namespace( sUnz ).CopyHere( psh.Namespace( sZip ).items, 4|16 )
	}
return
14.3 & 1.3.7
User avatar
JoeWinograd
Posts: 2209
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

10 May 2022, 17:31

flyingDman wrote:This is the only code you'll need
Tested here...the native Unz() function worked perfectly!

Question for @flyingDman: Is there a comparable native Zip() function? Thanks, Joe
lexikos
Posts: 9635
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

10 May 2022, 22:20

FYI, FileCopyDir supports a zip file as the source "directory" in v1.1.34.

I figured since the shell objects can be used to extract a zip, surely the shell function FileCopyDir uses can too. I was right, but FileCopyDir was checking if the source is a directory before attempting the copy.

Unfortunately it really can't work the other way (copying into a zip).
AlFlo
Posts: 359
Joined: 29 Nov 2021, 21:46

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

10 May 2022, 22:32

It seems like this is a situation where I have a weird setting on my computer which is preventing the script from working. Still nothing but just a couple of 0 byte files in temp files.
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

11 May 2022, 00:38

@Lexicos Thank you for pointing this out. I should check the change log more often ...
14.3 & 1.3.7
User avatar
JoeWinograd
Posts: 2209
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

11 May 2022, 03:12

lexikos wrote:FYI, FileCopyDir supports a zip file as the source "directory" in v1.1.34.
Didn't know that...nice!
AlFlo
Posts: 359
Joined: 29 Nov 2021, 21:46

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

11 May 2022, 10:51

Could my software version be the source of it not working for me? I'm using AHK 1.1.33.10 and Windows 10.
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: How to Extract all Zipped Files In a Directory While Preserving Folder Structure

11 May 2022, 10:55

Yes. Upgrade to 1.1.34 if you want to use FileCopyDir for this.
14.3 & 1.3.7

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: docterry, Leonardo_Portela, sofista and 176 guests