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.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Batch Script

  • Run Batch Script in C#
  • Replace Text From File in Batch Script
  • Run Batch Script in Silent Mode