WinRar and 7zip command to extract archive in new folder Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

WinRar and 7zip command to extract archive in new folder

Post by Elermino » 12 Sep 2023, 19:00

Good morning
I would like to know if there is a command in WinRar and 7zip that allows me to extract a compressed file into a new folder with the same file name, equivalent to right-clicking and selecting the <<extract in 'filename'/>> option. At the moment I only have the command in which you need to specify the folder in which the file will be extracted

Code: Select all

app        := A_ProgramFiles "\WinRAR\WinRAR.exe"
destDir    :=  "C:\Users\heseb\Downloads"
to         := " " 

; In WinRar:
RunWait % app " x """ fromArchiveFile """ *.* " to destDir ,, Hide

; In 7zip:
RunWait % app " x """ fromArchiveFile """ -o" to destDir,, Hide
What I have tried (for .zip files):

Code: Select all

RunWait % app " e " "%1 """ fromArchiveFile """ -o" to "C:\temp\%1" " -r",, Hide
(No result)

There should be 2 different commands, one for .rar and .zip files, since they are extracted with WinRaR and 7zip respectively

Complete script that extracts compressed files:
- getSelected() → Get the paths of selected files and folders both in Explorer and on the Desktop
- Detailed information of this scriptviewtopic.php?f=76&t=121149

Code: Select all

#SingleInstance force

destDir    :=  "C:\Users\heseb\Downloads"
;theseFiles7 := " "      ; 7zip
theseFiles := " *.* "   ; WinRAR
app7        := "C:\Program Files\7-Zip\7z.exe"
app        := A_ProgramFiles "\WinRAR\WinRAR.exe"
extract    := " x "
;irq     := " %1 "
;to7         := " -o"     ; 7zip
to         := " "      ; WinRAR

#If WinActive("ahk_exe explorer.exe")
!F2::
For each, fromArchiveFile in getSelected()
 If (fromArchiveFile ~= "i)\.zip$")
RunWait % app " x """ fromArchiveFile """ -o" to destDir ,, Hide
;~ RunWait % app " e " "%1 """ fromArchiveFile """ -o" to "C:\temp\%1" " -r",, Hide

If (fromArchiveFile ~= "i)\.rar$")
RunWait % app " x """ fromArchiveFile """ *.* " to destDir ,, Hide


;~ MsgBox 64, Status, Done!
Return
#If

getSelected() { ;  Get the paths of selected files and folders both in Explorer and on the Desktop
 ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 ; GEV: https://www.autohotkey.com/boards/viewtopic.php?p=514288#p514288
 Static SWC_DESKTOP := 8, SWFO_NEEDDISPATCH := 1
 WinGetClass winClass, % "ahk_id" hWnd := WinExist("A")
 If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass")
  Return
 shellWindows := ComObjCreate("Shell.Application").Windows, sel := []
 If !(winClass ~= "Progman|WorkerW") {
  For window in shellWindows
   If (hWnd = window.HWND) && (shellFolderView := window.Document)
    Break
 } Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP, 0, SWFO_NEEDDISPATCH).Document
 For item in shellFolderView.SelectedItems
  sel.Push(item.Path)
 Return sel
}
I think that with the commands provided by WinRaR and 7zip I can achieve this, thanks for any information :)

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

Re: WinRar and 7zip command to extract archive in new folder

Post by mikeyww » 12 Sep 2023, 19:56

Since you know the archive's path, you can use :arrow: SplitPath to get its parts. You can then append the filename without extension to the target directory name or stem (followed by "\"), to obtain your new directory path.

User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Re: WinRar and 7zip command to extract archive in new folder

Post by Elermino » 12 Sep 2023, 22:05

mikeyww wrote:
12 Sep 2023, 19:56
Since you know the archive's path, you can use :arrow: SplitPath to get its parts. You can then append the filename without extension to the target directory name or stem (followed by "\"), to obtain your new directory path.
Thanks for your help once again, maybe like this on line 1 and 13:

Code: Select all

destDir    :=  "C:\Users\heseb\Downloads" "" shrt\ ""

#If WinActive("ahk_exe explorer.exe")
!F2::
For each, fromArchiveFile in getSelected()
 If (fromArchiveFile ~= "i)\.zip$")
RunWait % app " x """ fromArchiveFile """ -o" to destDir ;,, Hide
;~ RunWait % app " e " "%1 """ fromArchiveFile """ -o" to "C:\temp\%1" " -r",, Hide

If (fromArchiveFile ~= "i)\.rar$")
RunWait % app " x """ fromArchiveFile """ *.* " to destDir ,, Hide

shrt := SplitPath, fromArchiveFile, name_no_ext
;~ MsgBox 64, Status, Done!
Return
#If

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

Re: WinRar and 7zip command to extract archive in new folder

Post by mikeyww » 13 Sep 2023, 04:23

You tested your script?

Code: Select all

#Requires AutoHotkey v1.1.33
dir := StrReplace(A_Desktop, "Desktop", "Downloads")
app := "d:\utils\7zip\x64\7za.exe"

#If WinActive("ahk_class CabinetWClass")
!F2::
For each, archiveFile in getSelected() {
 SplitPath archiveFile,,, ext, fnBare
 destDir := dir "\" fnBare
 Switch ext {
  Case "zip": RunWait % app " x """ archiveFile """ -o""" destDir """ *.* -r",, Hide
 }
}
MsgBox 64, Status, Done!
Return
#If

User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Re: WinRar and 7zip command to extract archive in new folder

Post by Elermino » 13 Sep 2023, 18:25

mikeyww wrote:
13 Sep 2023, 04:23
You tested your script?

Code: Select all

#Requires AutoHotkey v1.1.33
dir := StrReplace(A_Desktop, "Desktop", "Downloads")
app := "d:\utils\7zip\x64\7za.exe"

#If WinActive("ahk_class CabinetWClass")
!F2::
For each, archiveFile in getSelected() {
 SplitPath archiveFile,,, ext, fnBare
 destDir := dir "\" fnBare
 ...
#If
Sorry, I'm having difficulties. Does the destination file have to haven't whitespaces?
I get a message saying that there are no files to extract, however in a test MsgBox everything appears correctly

User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Re: WinRar and 7zip command to extract archive in new folder

Post by Elermino » 13 Sep 2023, 18:27

Elermino wrote:
13 Sep 2023, 18:25
mikeyww wrote:
13 Sep 2023, 04:23
You tested your script?

Code: Select all

#Requires AutoHotkey v1.1.33
dir := StrReplace(A_Desktop, "Desktop", "Downloads")
app := "d:\utils\7zip\x64\7za.exe"

#If WinActive("ahk_class CabinetWClass")
!F2::
For each, archiveFile in getSelected() {
 SplitPath archiveFile,,, ext, fnBare
 destDir := dir "\" fnBare
 ...
#If
Sorry, I'm having difficulties. The destination file don't have to have whitespaces?
I get a message saying that there are no files to extract, however in a test MsgBox everything appears correctly

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

Re: WinRar and 7zip command to extract archive in new folder

Post by mikeyww » 13 Sep 2023, 18:35

Are you running the script that I posted, or a different one?

User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Re: WinRar and 7zip command to extract archive in new folder

Post by Elermino » 13 Sep 2023, 19:34

mikeyww wrote:
13 Sep 2023, 18:35
Are you running the script that I posted, or a different one?
It already worked, I was trying with another :facepalm:
Thank you very much, with this I already have my script complete :wave:

Edit: Excuse me, for WinRar (.rar files) What would the extraction command be like?

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

Re: WinRar and 7zip command to extract archive in new folder  Topic is solved

Post by mikeyww » 13 Sep 2023, 20:52

UnZip or UnRAR

Code: Select all

; This script extracts files from ZIP or RAR archives
; More: https://autohotkey.com/board/topic/60706-native-zip-and-unzip-xpvista7-ahk-l/
#Requires AutoHotkey v1.1.33
SetTitleMatchMode RegEx
dir := StrReplace(A_Desktop, "Desktop", "Downloads")
rar := A_ProgramFiles "\WinRAR\WinRAR.exe"
zip := "d:\utils\7zip\x64\7za.exe"

#If WinActive("ahk_class Progman|WorkerW|(Cabinet|Explore)WClass")
!F2::
For each, archiveFile in getSelected() {
 SplitPath archiveFile,,, ext, fnBare
 destDir := dir "\" fnBare
 Switch ext {
  Case "zip": RunWait % zip " x """    archiveFile """ -o""" destDir """ *.* -r",, Hide
  Case "rar": RunWait % rar " x -r """ archiveFile """ """   destDir "\"""      ,, Hide
 }
}
MsgBox 64, Status, Done!
Return
#If

getSelected() { ;  Get the paths of selected files and folders both in Explorer and on the Desktop
 ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 ; GEV: https://www.autohotkey.com/boards/viewtopic.php?p=514288#p514288
 Static SWC_DESKTOP := 8, SWFO_NEEDDISPATCH := 1
 WinGetClass winClass, % "ahk_id" hWnd := WinExist("A")
 If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass")
  Return
 shellWindows := ComObjCreate("Shell.Application").Windows, sel := []
 If !(winClass ~= "Progman|WorkerW") {
  For window in shellWindows
   If (hWnd = window.HWND) && (shellFolderView := window.Document)
    Break
 } Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP, 0, SWFO_NEEDDISPATCH).Document
 For item in shellFolderView.SelectedItems
  sel.Push(item.Path)
 Return sel
}

User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Re: WinRar and 7zip command to extract archive in new folder

Post by Elermino » 13 Sep 2023, 21:41

mikeyww wrote:
13 Sep 2023, 20:52
UnZip or UnRAR

Code: Select all

; This script extracts files from ZIP or RAR archives
; More: https://autohotkey.com/board/topic/60706-native-zip-and-unzip-xpvista7-ahk-l/
#Requires AutoHotkey v1.1.33
SetTitleMatchMode RegEx
dir := StrReplace(A_Desktop, "Desktop", "Downloads")
rar := A_ProgramFiles "\WinRAR\WinRAR.exe"
zip := "d:\utils\7zip\x64\7za.exe"

#If WinActive("ahk_class Progman|WorkerW|(Cabinet|Explore)WClass")
!F2::
For each, archiveFile in getSelected() {
 SplitPath archiveFile,,, ext, fnBare
 destDir := dir "\" fnBare
 Switch ext {
  Case "zip": RunWait % zip " x """    archiveFile """ -o""" destDir """ *.* -r",, Hide
  Case "rar": RunWait % rar " x -r """ archiveFile """ """   destDir "\"""      ,, Hide
 }
}
MsgBox 64, Status, Done!
Return
#If

getSelected() { ;  Get the paths of selected files and folders both in Explorer and on the Desktop
 ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 ; GEV: https://www.autohotkey.com/boards/viewtopic.php?p=514288#p514288
 Static SWC_DESKTOP := 8, SWFO_NEEDDISPATCH := 1
 WinGetClass winClass, % "ahk_id" hWnd := WinExist("A")
 If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass")
  Return
 shellWindows := ComObjCreate("Shell.Application").Windows, sel := []
 If !(winClass ~= "Progman|WorkerW") {
  For window in shellWindows
   If (hWnd = window.HWND) && (shellFolderView := window.Document)
    Break
 } Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP, 0, SWFO_NEEDDISPATCH).Document
 For item in shellFolderView.SelectedItems
  sel.Push(item.Path)
 Return sel
}
Excellent as always, thanks :beer:

Post Reply

Return to “Ask for Help (v1)”