Extracting all ZIP files in child folders

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
papusale
Posts: 12
Joined: 18 Aug 2015, 11:24

Extracting all ZIP files in child folders

21 Aug 2015, 15:00

I would like to know how to extract all ZIP files in the specified folder's child folders (ex. "C:\Example" directory's ZIP files in all child folders).

The files should be extracted to the same folder where the ZIP file is situated (ex. "C:\Example\Sub1\File.zip" would extract all to "C:\Example\Sub1\")
Additionally, the ZIP file should be deleted after completion of extraction.

I would be much obliged if somebody could lend a hand in this. Thank you in advance! :)
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Extracting all ZIP files in child folders

29 Aug 2015, 07:46

you can use the a command line program like 7z.exe included with 7-zip.
something like this

Code: Select all

ZipFile_path := "" ; your zip file here
7z_Path := "C:\Program Files\7-Zip\7z.exe" ; the 7z.exe file to extract file
if !FileExist(ZipFile_path) || !FileExist(7z_Path){
	MsgBox, 16, error, the zip file doesn't exist or the 7z.exe doesn't
	ExitApp
	}
SplitPath, ZipFile_path,, parentfolder ; get the parent folder

;    x = (Extract with full paths) command
; -aoa = Overwrite All existing files without prompt.
; -aos = Skip extracting of existing files. 
;   -o = (set Output directory) switch

RunWait, %7z_Path% x "%ZipFile_path%" -o"%parentfolder%"\ -aos,, hide ; the command line to extract archives to folder, aos switch to skip existing file
FileDelete, %ZipFile_path%
jiggunjer
Posts: 13
Joined: 16 Jan 2016, 04:00

Re: Extracting all ZIP files in child folders

17 Jan 2016, 04:33

I think MJs misunderstood OP. All the above does is extract a single zip in a rather verbose manner. A 2 line batch file could do the same. But what OP (I think) meant was: "given a folder structure containing zip files, how to extract each zip file to the subfolder it is currently in?"
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Extracting all ZIP files in child folders

17 Jan 2016, 14:57

I have not tested it myself, but I believe MJs' code could be modified as follows to accomplish the original poster's goal...

Code: Select all

7z_Path := "C:\Program Files\7-Zip\7z.exe" ; the 7z.exe file to extract file
Source_Folder := "C:\Example"
Loop, %Source_Folder%\*.ZIP, 0, 1
{
ZipFile_path := A_LoopFileLongPath
if !FileExist(ZipFile_path) || !FileExist(7z_Path){
	MsgBox, 16, error, the zip file doesn't exist or the 7z.exe doesn't
	ExitApp
	}
SplitPath, ZipFile_path,, parentfolder ; get the parent folder
 
;    x = (Extract with full paths) command
; -aoa = Overwrite All existing files without prompt.
; -aos = Skip extracting of existing files. 
;   -o = (set Output directory) switch
 
RunWait, %7z_Path% x "%ZipFile_path%" -o"%parentfolder%"\ -aos,, hide ; the command line to extract archives to folder, aos switch to skip existing file
FileDelete, %ZipFile_path%
}
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

Re: Extracting all ZIP files in child folders

09 May 2022, 14:55

I just tried using Shadowpheonix's script to unzip everything in a specified directory. But I have one question from reviewing the results:

Does the script delete the name of zipped folders?

In other words, my directory had something like this:

Bulletin#24
Case-Hotel_-_Drawings (1).zip
A400-LEVEL-B2-RCP-Rev.2.pdf
A400M-LEVEL-B1-RCP-Rev.2.pdf
A407B.1-ENLARGED-7TH-FLOOR-RCP-DIMENSION-PLAN---EAST-Rev.2.pdf
After unizipping the directory using Shadowpheonix's script, I ended up with this:

Bulletin#24
A400-LEVEL-B2-RCP-Rev.2.pdf
A400M-LEVEL-B1-RCP-Rev.2.pdf
A407B.1-ENLARGED-7TH-FLOOR-RCP-DIMENSION-PLAN---EAST-Rev.2.pdf
In other words, the name of the zipped subfolder that the .pdf files are contained in - Case-Hotel_-_Drawings (1).zip - disappeared.

Is there any way to preserve the name of the zipped folders, so the script would produce a result like this:

Bulletin#24
Case-Hotel_-_Drawings (1)
A400-LEVEL-B2-RCP-Rev.2.pdf
A400M-LEVEL-B1-RCP-Rev.2.pdf
A407B.1-ENLARGED-7TH-FLOOR-RCP-DIMENSION-PLAN---EAST-Rev.2.pdf
In other words, is there any way to preserve the file structure?

My theory is that this very last line of code before the closing curly bracket is the problem:

Code: Select all

FileDelete, %ZipFile_path%
Instead of FileDelete, maybe I can use regex simply to strip the ".zip" ending off the subfolder name?

Or am I wrong, and is the problem somewhere else in the code?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: dra3th, Mateusz53, Rohwedder, Spawnova and 268 guests