Extract compressed file 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

Extract compressed file

Post by Elermino » 05 Sep 2023, 09:32

I have WinRar installed and I would like to know if the program has command line functions to extract the compressed files using a hotkey with autohotkey. I haven't found this, so I've seen other alternatives for this, like:

https://www.autohotkey.com/board/topic/23219-extract-from-rar-075-090/
https://www.autohotkey.com/board/topic/86876-unrarhk-unrar-your-moviesseries-efortlessly-from-context-menu/

This only accepts .rar files:
viewtopic.php?t=55680

Thank you in advance
Last edited by Elermino on 10 Sep 2023, 16:27, edited 1 time in total.

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

Re: Extract compressed file

Post by mikeyww » 05 Sep 2023, 10:24

WinRAR -> Help -> Help topics -> Command line mode -> Command line syntax

You can then use the :arrow: Run command with your command line.

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

Re: Extract compressed file

Post by Elermino » 05 Sep 2023, 10:54

mikeyww wrote:
05 Sep 2023, 10:24
WinRAR -> Help -> Help topics -> Command line mode -> Command line syntax

You can then use the :arrow: Run command with your command line.
Thanks for your reply. How do I get it to detect if a file is selected in the file explorer or the desktop and extract it?

Maybe this is the correct syntax for the Run command?

Code: Select all

run, unrar e %FilePath%

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

Re: Extract compressed file

Post by mikeyww » 05 Sep 2023, 11:30

If you search forum for "ExplorerGetSelection", you should see the function by teadrinker that will do that part.

An adapted one: viewtopic.php?p=514288#p514288

Code: Select all

#Requires AutoHotkey v1.1.33
dir         := A_ScriptDir
fromRARfile := dir "\temp2.rar"
toDestDir   := dir "\t"
theseFiles  := "*.*"
; -------------------------------------------------------------------------
app         := A_ProgramFiles "\WinRAR\WinRAR.exe"
extract     := "x"
_           := " "
RunWait % app _ extract _ fromRARfile _ theseFiles _ toDestDir "\",, Hide
MsgBox 64, Status, Done!

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

Re: Extract compressed file

Post by Elermino » 06 Sep 2023, 08:01

mikeyww wrote:
05 Sep 2023, 11:30
If you search forum for "ExplorerGetSelection", you should see the function by teadrinker that will do that part.

An adapted one: viewtopic.php?p=514288#p514288

Code: Select all

#Requires AutoHotkey v1.1.33
dir         := A_ScriptDir
fromRARfile := dir "\temp2.rar"
...
Sorry, how can I get this script to work so that it detects the selected file and extracts it to a single folder?
I tried this but no result

Code: Select all

~!x::
;~ send, ^+x

file := getSelected()
dir         := A_ScriptDir
fromRARfile := dir "\" file
toDestDir   := dir "\t"
theseFiles  := "*.*"
; -------------------------------------------------------------------------
app         := A_ProgramFiles "\WinRAR\WinRAR.exe"
extract     := "x"
_           := " "
RunWait % app _ extract _ fromRARfile _ theseFiles _ toDestDir "\",, Hide
MsgBox 64, Status, Done!


getSelected(){
	; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
	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 := 8, 0, SWFO_NEEDDISPATCH := 1).Document
	For item in shellFolderView.SelectedItems
		sel.Push(item.Path)
	Return sel
}

GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Extract compressed file

Post by GEV » 06 Sep 2023, 09:13

That function on
viewtopic.php?p=514288#p514288
didn't always work.
I replaced it by this one:

Code: Select all

; Get path of selected files/folders:
getSelected(){
	; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=60403&p=255273#p255256
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
      Return	  
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")
      shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
   else {
      for window in shellWindows
       try  if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   for item in shellFolderView.SelectedItems
      result .= (result = "" ? "" : "`n") . item.Path
   if !result
      result := shellFolderView.Folder.Self.Path
   Return result
}

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

Re: Extract compressed file

Post by mikeyww » 06 Sep 2023, 09:23

If your paths contain spaces, you might need to add some quotation marks to your command line.

Code: Select all

#Requires AutoHotkey v1.1.33
toDestDir  := A_ScriptDir "\t"
theseFiles := "*.*"
app        := A_ProgramFiles "\WinRAR\WinRAR.exe"
extract    := "x"
_          := " "

#If WinActive("ahk_class CabinetWClass")
!x::
For each, fromRARfile in getSelected()
 If (fromRARfile ~= "i)\.rar$")
  RunWait % app _ extract _ fromRARfile _ theseFiles _ toDestDir "\",, 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
}

GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Extract compressed file

Post by GEV » 06 Sep 2023, 11:07

mikeyww wrote:
06 Sep 2023, 09:23

Code: Select all

getSelected() { ;  Get the paths of selected files and folders both in Explorer and on the Desktop
...
 Return sel
}
Unfortunately it doesn't work here:

Code: Select all

F1:: MsgBox, % getSelected()

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
}
Tested on both Windows 10 and Windows 11.

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

Re: Extract compressed file

Post by mikeyww » 06 Sep 2023, 11:17

An array is returned, so try the enumerating script that I posted.

GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Extract compressed file

Post by GEV » 06 Sep 2023, 12:20

mikeyww wrote:
06 Sep 2023, 11:17
An array is returned, so try the enumerating script that I posted.
Ok, thanks. It works for that case.

Code: Select all

#If WinActive("ahk_class CabinetWClass") || WinActive("Program Manager") 

	F1::
		For each, TXTfile in getSelected()
			If (TXTfile ~= "i)\.txt$")
				RunWait % TXTfile
		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
    try  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
  try  sel.Push(item.Path)
 Return sel
}
But not for getting the path

Code: Select all

F1:: MsgBox, % getSelected()
or for copying the path

Code: Select all

clipboard := getSelected()
of selected items.

I mean, to avoid confusion it should have another name.

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

Re: Extract compressed file

Post by mikeyww » 06 Sep 2023, 13:03

You can use SplitPath for any item. Will be the same for all of them.

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

Re: Extract compressed file

Post by Elermino » 06 Sep 2023, 17:02

mikeyww wrote:
06 Sep 2023, 13:03
You can use SplitPath for any item. Will be the same for all of them.
Thank you very much for your help, I am pleased that this topic is of interest to you. :clap:
The selected .zip file is still not extracted, any recommendation? :think:

Thanks in advance

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

Re: Extract compressed file

Post by mikeyww » 06 Sep 2023, 18:31

Yes, I recommend that you post the revised script that you are currently using.

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

Re: Extract compressed file

Post by Elermino » 06 Sep 2023, 19:40

mikeyww wrote:
06 Sep 2023, 18:31
Yes, I recommend that you post the revised script that you are currently using.
Is this:

Code: Select all

toDestDir  := A_ScriptDir
theseFiles := "*.*"
app        := A_ProgramFiles "\WinRAR\WinRAR.exe"
extract    := "x"
_          := " "

#If WinActive("ahk_class CabinetWClass")
!F2::
For each, fromRARfile in getSelected()
 If (fromRARfile ~= "i)\.rar$")
  RunWait % app _ extract _ fromRARfile _ theseFiles _ toDestDir "\",, 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
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Extract compressed file

Post by mikeyww » 06 Sep 2023, 20:23

Below is an update for Zip. Use a registered copy of WinRAR. 7zip is a free alternative if you just want to unpack.

Code: Select all

#Requires AutoHotkey v1.1.33
destDir    := A_ScriptDir "\t\"
theseFiles := " "      ; 7zip
theseFiles := " *.*"   ; WinRAR
app        := "d:\utils\7zip\x64\7za.exe"
app        := A_ProgramFiles "\WinRAR\WinRAR.exe"
extract    := " x "
to         := "-o"     ; 7zip
to         := " "      ; WinRAR

#If WinActive("ahk_class CabinetWClass")
!x::
For each, fromArchiveFile in getSelected()
 If (fromArchiveFile ~= "i)\.(rar|zip)$")
  RunWait % app extract fromArchiveFile theseFiles 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
}

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

Re: Extract compressed file

Post by Elermino » 06 Sep 2023, 22:04

mikeyww wrote:
06 Sep 2023, 20:23
Below is an update for Zip. Use a registered copy of WinRAR. 7zip is a free alternative if you just want to unpack.

Code: Select all

#Requires AutoHotkey v1.1.33
destDir    := A_ScriptDir "\t\"
theseFiles := " "      ; 7zip
theseFiles := " *.*"   ; WinRAR
app        := "d:\utils\7zip\x64\7za.exe"
app        := A_ProgramFiles "\WinRAR\WinRAR.exe"
extract    := " x "
to         := "-o"     ; 7zip
to         := " "      ; WinRAR

#If WinActive("ahk_class CabinetWClass")
!x::
For each, fromArchiveFile in getSelected()
 If (fromArchiveFile ~= "i)\.(rar|zip)$")
  RunWait % app extract fromArchiveFile theseFiles to destDir,, Hide
MsgBox 64, Status, Done!
Return
#If
...
}
Amazing script, thank you sir :thumbup:

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

Re: Extract compressed file

Post by Elermino » 10 Sep 2023, 12:40

mikeyww wrote:
06 Sep 2023, 20:23
Below is an update for Zip. Use a registered copy of WinRAR. 7zip is a free alternative if you just want to unpack.

Code: Select all

#Requires AutoHotkey v1.1.33
destDir    := A_ScriptDir "\t\"
theseFiles := " "      ; 7zip
theseFiles := " *.*"   ; WinRAR
app        := "d:\utils\7zip\x64\7za.exe"
app        := A_ProgramFiles "\WinRAR\WinRAR.exe"
extract    := " x "
to         := "-o"     ; 7zip
to         := " "      ; WinRAR

#If WinActive("ahk_class CabinetWClass")
!x::
For each, fromArchiveFile in getSelected()
 ...
}
Hello. Sorry for opening this topic again, but I have not been able to get the .zip files to be extracted, since I already installed 7zip and set the directory to '7z.exe'. I changed a bit of your code but there are no results, with the original code I get a box saying 'No files found'. I ask for one last help to solve this problem please.
Thank you!

My attempt:

Code: Select all

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

#If WinActive("ahk_class CabinetWClass")
!x::
For each, fromArchiveFile in getSelected()
 If (fromArchiveFile ~= "i)\.rar$") ; Change this to fist case rar else if zip
  RunWait % app extract fromArchiveFile theseFiles to destDir,, Hide

 else if (fromArchiveFile ~= "i)\.zip$")
   RunWait % app7 extract fromArchiveFile theseFiles7 to7 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
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Extract compressed file

Post by mikeyww » 10 Sep 2023, 12:44

If you change RunWait to MsgBox, I think you will find that a space is missing in your command line.

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

Re: Extract compressed file

Post by Elermino » 10 Sep 2023, 12:57

mikeyww wrote:
10 Sep 2023, 12:44
If you change RunWait to MsgBox, I think you will find that a space is missing in your command line.
I don't see it clearly, where exactly is that space missing? :think:

Image

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

Re: Extract compressed file

Post by mikeyww » 10 Sep 2023, 13:46

Are you running the script that you posted? Does not look right or match what I see. Zip files should be using 7z, according to your script.

This worked for me when my path had spaces.

Code: Select all

RunWait % app7 extract """" fromArchiveFile """" theseFiles7 to7 destDir,, Hide
Perhaps the variables complicate too much. Can simplify:

Code: Select all

RunWait % app7 " x """ fromArchiveFile """ -o" destDir,, Hide

Post Reply

Return to “Ask for Help (v1)”