Unzip or Unrar files with Batch file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Unzip or Unrar files with Batch file?

#16 Post by Liviu » 12 Apr 2012 22:13

Fawers wrote:But does it support command line operations?

Yes, unrar.exe is a console app, fully (and only) command line.

Fawers wrote:I think the standard 7zip installation file comes with 7z.dll already. At least I have it here in 7-Zip folder, and I can't remember of downloading any additional patch/update. I don't have 7za.exe, though

You are correct. My note of 7za.exe was more for fans of self-contained/copy-safe utilities, who don't want/need to remember that for example copying 7z.exe to a USB stick requires 7z.dll to go along with it.

Liviu

miskox
Posts: 356
Joined: 28 Jun 2010 03:46

Re: Unzip or Unrar files with Batch file?

#17 Post by miskox » 13 Apr 2012 02:40

There is a FREE official unRAR console application for Win 32:

http://www.rarlab.com/rar/unrarw32.exe

I am sure it can handle wildcard characters (I don't use it).

As mentioned here viewtopic.php?p=14704#p14704 I use unzip.exe (size of 159744 bytes).

I created three .zip files and if I use

Code: Select all

unzip *.zip


they all unzip successfully. Question remains about the subfolders/files: should they be overwritten or not?

Saso

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Unzip or Unrar files with Batch file?

#18 Post by Fawers » 13 Apr 2012 10:43

miskox wrote:Question remains about the subfolders/files: should they be overwritten or not?

Saso

I'm sure that could be a problem. 7z and WinRAR standard command line applications ask whether user wants to overwrite files or not. They even ask for a password if they found out that the files are encrypted.

I'm gonna test anyways. Thanks for the suggestion.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Unzip or Unrar files with Batch file?

#19 Post by Fawers » 14 Apr 2012 10:26

ivar75,

This is tested. It works with WinRAR command line application for *.rar files and 7-Zip command line application for *.7z and *.zip files.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set rarpath=
set 7zpath=
::If it is a .rar file that you want to unrar, set ¦rarpath¦ to rar.exe
::rar.exe can probably be found inside %programfiles%\winrar
::If it is, however, a .zip or a .7z file, ¦7zpath¦ must point to 7z.exe
::7z.exe can probably be found inside %programfiles%\7-Zip
for %%d in ("%cd%") do (set foldername=%%~nd)
for %%f in (rar zip 7z) do (
  if exist *.%%f (
    call :extract%%f "%%~f"
  ) else (
    echo No %%f files were found in %foldername%.
>nul ping -n 4 localhost
    )
  )
endlocal
exit /b

:EXTRACTRAR
"%rarpath%" e "*.rar"
exit /b

:EXTRACTZIP
::this section is here merely to avoid errors with FOR loop.
::since the process is the same as in 7z, it will skip through.
goto EXTRACT7Z

:EXTRACT7Z
"!7zpath!" e "*.%~1"
exit /b


In case you don't have WinRAR and/or 7-Zip, you can download them here and here, respectively.

redbob1985
Posts: 2
Joined: 10 May 2020 04:34

Re: Unzip or Unrar files with Batch file?

#20 Post by redbob1985 » 10 May 2020 04:36

Fawers wrote:
14 Apr 2012 10:26
ivar75,

This is tested. It works with WinRAR command line application for *.rar files and 7-Zip command line application for *.7z and *.zip files.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set rarpath=
set 7zpath=
::If it is a .rar file that you want to unrar, set ¦rarpath¦ to rar.exe
::rar.exe can probably be found inside %programfiles%\winrar
::If it is, however, a .zip or a .7z file, ¦7zpath¦ must point to 7z.exe
::7z.exe can probably be found inside %programfiles%\7-Zip
for %%d in ("%cd%") do (set foldername=%%~nd)
for %%f in (rar zip 7z) do (
  if exist *.%%f (
    call :extract%%f "%%~f"
  ) else (
    echo No %%f files were found in %foldername%.
>nul ping -n 4 localhost
    )
  )
endlocal
exit /b

:EXTRACTRAR
"%rarpath%" e "*.rar"
exit /b

:EXTRACTZIP
::this section is here merely to avoid errors with FOR loop.
::since the process is the same as in 7z, it will skip through.
goto EXTRACT7Z

:EXTRACT7Z
"!7zpath!" e "*.%~1"
exit /b

In case you don't have WinRAR and/or 7-Zip, you can download them here and here, respectively.
Hi,

This works perfectly to extract to subfolder, by changing "e" to "x" to extrac tto subfolder in same directory based on the name

I am wondering if anyone can help to modify this working script to delete the rar files after successful extraction?

Please help if you can and thanks in advance

ShadowThief
Expert
Posts: 984
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Unzip or Unrar files with Batch file?

#21 Post by ShadowThief » 10 May 2020 11:53

Code: Select all

command1&&command2
only runs command2 if command1 is successful, so you can say

Code: Select all

for %%A in (*.rar) do (
	"%rarpath%" x "%%~A" && del "%%~A"
)

redbob1985
Posts: 2
Joined: 10 May 2020 04:34

Re: Unzip or Unrar files with Batch file?

#22 Post by redbob1985 » 11 May 2020 02:21

ShadowThief wrote:
10 May 2020 11:53

Code: Select all

command1&&command2
only runs command2 if command1 is successful, so you can say

Code: Select all

for %%A in (*.rar) do (
	"%rarpath%" x "%%~A" && del "%%~A"
)
This works perfectly, such a small script and does just what I need.

Thank you so, so much for sharing this and helping me! I greatly appreciate it!

Post Reply