5

So after digging around for a day or so I have found not much information about powershell and extracting files with winrar's command line.

My difficulty lies with the actual execution of the arguments and command line.

Here is my simple code so far:

$WinRar = "C:\Program Files\WinRAR\winrar.exe"

#filter through files looking for zip files.
$path = get-ChildItem -filter "*.zip" -path A:\testfolder\ -recurse

#test to see if it is actually filtering and showing correct results:
write-host = $path | format-table directory

#arguments :: x (extract) y(presume yes) r(recurse subdir) 
&$WinRar x -y -r "$path" A:\Testfolder\



read-host "press to exit"

I'm pretty new to powershell and seem to be catching on pretty quick but I'm assuming I have got this completely wrong as the code executes but it doesn't extract. So I guess it errors out somewhere with the command line and I cant see what it is doing.

Are they any suggestions or tips that can point me in the right direction?

I have tried many variations of the $winrar command with no luck except for the part if I replace $path with the actual file path it works.. which isn't what I need :P

Ben
  • 51
  • 1
  • 1
  • 2
  • As I can judge from WinRAR help, WinRAR does not allow specify multiple archive names in command line. You have to extract archives one by one in a loop. – user4003407 Jan 11 '16 at 11:21
  • It is not possible to specify on command line of *WinRAR* multiple file names of archives to extract all or just specified files/folders from. But it is possible to extract files/folders from all archives matching a file name pattern containing the wildcards `*` or `?`. For example `WinRAR x *.zip` extracts all ZIP archives in current directory into current directory. With *WinRAR* v5.30 or any later version `WinRAR x *\*.zip` extracts from all ZIP files in any subdirectory all files/folders into current directory, but ZIP archives in current directory are ignored. – Mofi Jan 16 '16 at 16:14

2 Answers2

5

You can use simple foreach method for this:

$Zips = Get-ChildItem -filter "*.zip" -path A:\testfolder\ -Recurse
$Destination = 'C:\Extracted'
$WinRar = "C:\Program Files\WinRAR\winrar.exe"

foreach ($zip in $Zips)
{
&$Winrar x $zip.FullName $Destination
Get-Process winrar | Wait-Process
}

If you want multiple WinRAR processes running together, remove the Get-Process line

Avshalom
  • 8,657
  • 1
  • 25
  • 43
  • 1
    Unlike CMD, PowerShell will not wait for completion of GUI program even when program called from script. So your command will spawn multiple concurrent WinRAR instances. – user4003407 Jan 11 '16 at 11:39
  • @Avshalom If I was to decide to keep the extracted file in the same folder it was found how would I go about that? If i was to guess: &$Winrar x $zip.FullName $zip.directory ?? Thanks in advance! – Ben Jan 11 '16 at 12:10
  • 1
    you can create a directory for each zip, also you can create a CSV file with 2 columns, zipname and destination folder and load it from there – Avshalom Jan 11 '16 at 12:27
  • Open in *WinRAR* __Help topics__ from menu __Help__, select tab __Contents__, open item __Command line mode__ and next __Switches__ and read the page about the switch `-ad`. For example using `WinRAR x -ad "My Archive.zip"` results in extracting all files and folders of this archive into subdirectory `My Archive` in current directory with *WinRAR* creating the subdirectory `My Archive` automatically if not existing already. – Mofi Jan 16 '16 at 16:22
0

Use rar.exe instead of winrar.exe. It is in the same directory.

This will properly wait for the process to finish even if multiple are running.

mafu
  • 31,798
  • 42
  • 154
  • 247