2

I need to iterate a folder and for each zip file I need to extract it with the name of it. That is, if its test.zip then it should extract to test folder. Similarly it should iterate through my folder and its child folders and extract the things. I wrote the below code but it doesn't extract with the name of the zip. Please advice.

cd %CD%\Setups
for /r %%i in ("*.zip") do (
   echo "%%~fi"
    "C:\Program Files (x86)\WinRAR\WinRAR.exe" a -afzip "%%~dpi" "%%~fi"
    echo came after unzipping
    del /S /Q "%%~fi"
)
exit \b
Hackoo
  • 18,337
  • 3
  • 40
  • 70
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
  • What about `%~dpni` instead of `%~dpi`? – aschipfl Jun 27 '16 at 23:30
  • %~dpni is finding the current location it should extract to. But ~%fi is not giving me the folder name which should be created on the name of the zip. That is, test.zip file is not getting extracted to test folder. – AnOldSoul Jun 27 '16 at 23:55
  • `%%~dpi` points to the container of the `.zip` file, `%%~ni` is the base name of the `.zip` file (e. g., `test`), so `%%~dpni` points to the folder to extract; `%%~fi` points to the `.zip` file... – aschipfl Jun 28 '16 at 08:24
  • This task can be done without any batch code at all as *WinRAR* offers a switch for this very often needed kind of extraction. See answer on [How to unzip several \*.zip archive files into separate folders?](http://stackoverflow.com/a/38879219/3074564) The task can be done from within *WinRAR* GUI with a few clicks, or from within a command prompt window by executing a single command line or via context menu in Windows Explorer. – Mofi Sep 18 '16 at 18:03

1 Answers1

3

The syntax for extracting zip files with Winrar.exe is like that :

[path\winrar.exe] x [path to zip file] [files to extract, . for all files] [path folder to extract to]

@echo off
set winrar=%ProgramFiles%\WinRAR\WinRAR.exe
CD /D %CD%\Setups
for /f "delims=" %%i in ('Dir /b *.zip') Do ("%Winrar%" x "%%~fi" "%%~dpni\")
pause

Edit on 28/06/2016 @ 13:35

You can also add if you want this switch -ibck to run WinRAR in background :

@echo off
set winrar=%ProgramFiles%\WinRAR\WinRAR.exe
CD /D %CD%\Setups
for /r %%i in ("*.zip") Do ("%Winrar%" x -ibck "%%~fi" "%%~dpni\")
pause
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • the for loop says "file not found" when used in the way you've specified. So I used "for /r %%i in ("*.zip")" and it works fine. Can I know why the for loop you've specified is not working :( – AnOldSoul Jun 28 '16 at 03:53
  • @mayooran I tested my script before i posted to you and it works for me like a charm :) just i used `set winrar=%ProgramFiles%\WinRAR\WinRAR.exe` which perhaps is diffrent for you `set winrar=%ProgramFiles (x86)%\WinRAR\WinRAR.exe` – Hackoo Jun 28 '16 at 06:48
  • no actually it was coming from the for loop. I removed line by line to find the line which was throwing that file not found error. I'll check again. Thanks :) – AnOldSoul Jun 28 '16 at 07:22
  • @mayooran Check my last edit for running WinRAR in background with this switch `-ibck` – Hackoo Jun 28 '16 at 12:35
  • now its working fine thanks :) would you be also able to help with this one? http://stackoverflow.com/questions/38088275/zipping-all-files-and-subdirectories-inside-a-folder-using-batch-file – AnOldSoul Jun 29 '16 at 00:14