Page 2 of 2
Re: Unzip or Unrar files with Batch file?
Posted: 12 Apr 2012 22:13
by Liviu
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
Re: Unzip or Unrar files with Batch file?
Posted: 13 Apr 2012 02:40
by miskox
There is a FREE official unRAR console application for Win 32:
http://www.rarlab.com/rar/unrarw32.exeI 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
they all unzip successfully. Question remains about the subfolders/files: should they be overwritten or not?
Saso
Re: Unzip or Unrar files with Batch file?
Posted: 13 Apr 2012 10:43
by Fawers
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.
Re: Unzip or Unrar files with Batch file?
Posted: 14 Apr 2012 10:26
by Fawers
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.
Re: Unzip or Unrar files with Batch file?
Posted: 10 May 2020 04:36
by redbob1985
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
Re: Unzip or Unrar files with Batch file?
Posted: 10 May 2020 11:53
by ShadowThief
only runs command2 if command1 is successful, so you can say
Code: Select all
for %%A in (*.rar) do (
"%rarpath%" x "%%~A" && del "%%~A"
)
Re: Unzip or Unrar files with Batch file?
Posted: 11 May 2020 02:21
by redbob1985
ShadowThief wrote: ↑10 May 2020 11:53
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!