Extract or Unzip File in Batch Script

MD Aminul Islam Jul 27, 2022
Extract or Unzip File in Batch Script

In this article, we will see how we can create a batch script to extract files from a zip file.

Create Batch Script To Unzip File

We can extract or unzip a file using Batch Script, where we need to include a destination directory where the file will be extracted and a directory of our source file. To unzip a zipped file, we need to integrate a different scripting language inside your Batch Script, the Visual Basic Script, shortly known as VBS.

Batch Script:

@ECHO off
CD /d %~dp0
CALL :UnZipFile "C:\ExtractLocation\" "C:\Sample\sample.zip"

: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%

In the code above, we called the :UnZipFile function. We integrated the VB script to include the unzipping functionality in the function.

You can notice that we passed two parameters to our function where the first parameter(<ExtractTo>) is for the directory where the file will be extracted, and the second parameter(<newzipfile>) is for the directory where the source file is.

When you run the above batch script by correctly providing all the necessary directories, you will get the zip file extracted in the directory you provide for extracting files.

Remember that the commands we discussed here are only for the Windows command prompt or CMD environment.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.


Related Article - Batch Script