JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Extract or unzip files in batch script

Author:JIYIK Last Updated:2025/03/21 Views:

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


Create a batch script to unzip the files

We can extract or decompress files using batch scripts which need to contain the destination directory where the files will be extracted and the directory of the source files. To decompress the compressed files, we need to integrate a different scripting language in the batch script, namely Visual Basic Script, or VBS for short.

Batch script:

@ECHO off
setlocal
CD /d %~dp0
CALL :UnZipFile "C:\ExtractLocation" "C:\Sample\sample.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%

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

You can notice that we pass two arguments to the function, where the first argument ( <ExtractTo>) is for the directory where the files are extracted and the second argument ( <newzipfile>) is for the directory where the source files are located.

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

请记住, the commands we discuss here apply only to the Windows Command Prompt, or CMD environment.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Transferring files to Raspberry Pi using batch script

Publish Date:2025/03/21 Views:56 Category:OPERATING SYSTEM

This article will show a batch command that transfers a file from Windows to the Raspberry Pi environment and executes the file. PuTTY is a third-party free tool that implements Telnet and SSH for Windows and Unix platforms for free. It als

Renaming part of a file name in a batch script

Publish Date:2025/03/21 Views:122 Category:OPERATING SYSTEM

Sometimes we need to rename a series of files in a specific order. Most project files contain this sequence, and we can easily do this in a batch script using a simple one-line command. This short article will show us how to change a specif

Run batch scripts using Task Scheduler

Publish Date:2025/03/21 Views:152 Category:OPERATING SYSTEM

This article will show you how to use Task Scheduler to run a batch file. Run batch scripts using Task Scheduler With Task Scheduler, you can automate tasks to run automatically at specific times. It only takes a few steps and you don't nee

Mapping a network drive in a batch script

Publish Date:2025/03/21 Views:69 Category:OPERATING SYSTEM

This article will discuss how to map a network drive in a batch script. Mapping a network drive in a batch script For this purpose, we will see three formats of the same command. However, the general format of the command is: net use P: "\\

Copy files using batch script

Publish Date:2025/03/21 Views:91 Category:OPERATING SYSTEM

Batch scripts are scripts built specifically for the Windows command line to perform a variety of tasks and execute user-directed sequences of commands. Batch scripts make it easy to use Windows PowerShell. This article will explain how to

Downloading files from URL in batch script

Publish Date:2025/03/21 Views:63 Category:OPERATING SYSTEM

Today we have several download managers to download necessary files from the Internet. In addition, there is a download manager integrated into every web browser. But you can create a batch script that can also perform a similar task of dow

Running a batch script in silent mode

Publish Date:2025/03/21 Views:112 Category:OPERATING SYSTEM

If you run CMD without silent mode, the command prompt will print every line of code in CMD. This article will show you how to activate CMD silent mode via batch script. We will also learn different ways to run batch scripts silently. We wi

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial