Saturday, February 02, 2019

How to Unzip Files Using a Batch File - Simple Step by Step Guide


Suppose you have a single zip file “RawData.zip” containing some other files placed in “Source Folder” in C: drive. And you want to unzip files using batch commands then follow below steps:


Step 1: Open Notepad in your system.


Step 2: Copy below code in Notepad:


REM Extracting Single Zip File

@echo off

setlocal

cd /d %~dp0


Call :UnZipFile "C:\Source Folder\" "C:\Source Folder\rawdata.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%



Step 4: Save Notepad file in “Source Folder” and give any name like “unzipfile.bat”.


Now you can run batch file and it will unzip rawdata.zip files in “Source Folder”.


Note:- If you want to edit batch file, then right click on it and then click on “Edit” or you can open batch file in Notepad and after editing you can save.





How to unzip multiple zip files using Batch Command?


Suppose you have multiple zip files with different names placed in “C:\Source Folder\” like below:


RawData.zip”,

RawData1.zip

newdata.zip

newdata1.zip


And you want to unzip all files in “C:\Source Folder\” then replace code as below:


‘From

Call :UnZipFile "C:\Source Folder\" "C:\Source Folder\rawdata.zip"


‘To

for %%a in (*.zip) do (

Call :UnZipFile "C:\Source Folder\" "C:\Source Folder\%%~nxa"

)



Or you can copy below code:


REM Extracting Multiple Zip Files

@echo off

setlocal

cd /d %~dp0


for %%a in (*.zip) do (

Call :UnZipFile "C:\Source Folder\" "C:\Source Folder\%%~nxa"

)


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%




How to unzip multiple files in separate folders using Batch Command?


Suppose you have multiple zip files with different names placed in “C:\Source Folder\”


RawData.zip”,

RawData1.zip

newdata.zip

newdata1.zip


And you want to unzip all files in their separate folders like below:


“C:\Source Folder\RawData\”

“C:\Source Folder\RawData1\”

“C:\Source Folder\newdata\”

“C:\Source Folder\newdata1\”


Then replace code as below:


‘From

Call :UnZipFile "C:\Source Folder\" "C:\Source Folder\rawdata.zip"


‘To

for %%a in (*.zip) do (

Call :UnZipFile "C:\Source Folder\%%~na\" "C:\Source Folder\%%~nxa"

)



Or you can copy below code:


REM Extracting Multiple Zip Files in Their Separate Folders

@echo off

setlocal

cd /d %~dp0


for %%a in (*.zip) do (

Call :UnZipFile "C:\Source Folder\%%~na\" "C:\Source Folder\%%~nxa"

)


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%






How to unzip files in sub- folders using Batch Command?


Copy above Batch file in all sub-folders and make another batch file to call all batch files in it.




***** End *****


No comments:

Post a Comment