• June 7, 2023
  •  

How to Unzip Files Using a Batch File

unzip-files-using-batch-file-simplemsoffice

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

Step 1: Open Notepad in your system.

Step 2: Copy the below code in Notepad:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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%

'Remove below line before running the batch file script
</newzipfile></extractto>

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

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

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

 

Unzip multiple zip files using Batch Command

If 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 the code in the line 6 as below:

From

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

To

1
2
3
for %%a in (*.zip) do (
Call :UnZipFile "C:\Source Folder" "C:\Source Folder\%%~nxa"
)

Or you can copy the below code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
REM Extracting Single Zip File
@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%

'Remove below line before running the batch file script
</newzipfile></extractto>

 

Unzip multiple files in separate folders using Batch Command

If 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 the code as below:
From

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

To

1
2
3
for %%a in (*.zip) do (
Call :UnZipFile "C:\Source Folder\%%~na" "C:\Source Folder\%%~nxa"
)

Or you can copy the below code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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%

'Remove below line before running the batch file script
</newzipfile></extractto>

 

0 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *