-1

The following code (adapted from https://stackoverflow.com/a/21709923/711006) unzips file named "batchX.zip" kept in folder 'Zipped'

@echo off
setlocal
cd /d %~dp0
Call :UnZipFile "D:\Unzipped\" "D:\Zipped\batchX.zip"
exit /b

:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs%  echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%

However, the file name has to be mentioned in the batch file (batchX.zip) for it to unzip the zip file.

Can we modify this batch code so that we do not need to mention the name, and any zip file placed can be unzipped?

Also, can we unzip a .rar file through a batch code?

Melebius
  • 6,183
  • 4
  • 39
  • 52
vicki
  • 157
  • 2
  • 23
  • https://stackoverflow.com/questions/21704041/creating-batch-script-to-unzip-a-file-without-additional-zip-tools – vicki Nov 29 '17 at 14:09
  • Thank you, please add the link directly to the question next time. I’ve done it for you this time. – Melebius Nov 29 '17 at 14:11
  • check this : https://stackoverflow.com/questions/28043589/how-can-i-compress-zip-and-uncompress-unzip-files-and-folders-with-bat – npocmaka Nov 29 '17 at 14:31

1 Answers1

0

Unzipping any file

Can we modify this batch code so that we do not need to mention the name, and any zip file placed can be unzipped ?

Batch files can utilize parameters, just like other scripting languages. The batch-file syntax is %1.

Call :UnZipFile "D:\Unzipped\" %1

Then call your batch-file with the parameter:

myscript.bat zipped-file.zip

Unrarring a file

Also, can we unzip a .rar file through a batch code?

Yes if you have the appropriate binary. You can download rar.exe (a part of WinRAR) and use it from a batch-file (see the manual for details), e.g.

rar e %1
Melebius
  • 6,183
  • 4
  • 39
  • 52
  • Thanks. I am not an expert like you. Can you modify the above code. – vicki Nov 29 '17 at 14:17
  • What code? If you want me to apply the solution to the code in your question, please note we don’t do it here. The question would look superfluous if it contained the solution. – Melebius Nov 29 '17 at 14:23