Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating batch script to unzip a file without additional zip tools

I'm trying to make a .bat script for windows 7 x64 to create a folder, unzip a file into that folder without having to use additional addons like 7zip or unzip. Been searching and it seemed like windows doesn't have builtins to allow unzip easily in command. Can I unzip/expand files without additional addons?

like image 992
JustinBieber Avatar asked Feb 11 '14 14:02

JustinBieber


People also ask

How do I unzip a file in CMD?

tar -xf filename.zip Rename filename with the actual name of the file. Now, hit Enter and the ZIP file will be extracted in the same folder. CMD has successfully unzipped the ZIP file in the same location.

How do I unzip a file in Windows 10 with command prompt?

- Type "unzip [file_location]\[filename. zip]" into the command prompt, replacing "[file_location]" with the full directory path and "[filename. zip]" with the name of the ZIP file you want to extract. Press the "Enter" key.

How zip a file using CMD?

Linux (and Mac terminal) command line The syntax is ' zip -r <zip file name> <directory name> '. The '-r' option tells zip to include files/folders in sub-directories. Use quotes around the zip file or folder name if the name contains any spaces.


1 Answers

Try this:

@echo off setlocal cd /d %~dp0 Call :UnZipFile "C:\Temp\" "c:\path\to\batch.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% 

Revision

To have it perform the unzip on each zip file creating a folder for each use:

@echo off setlocal cd /d %~dp0 for %%a in (*.zip) do (     Call :UnZipFile "C:\Temp\%%~na\" "c:\path\to\%%~nxa" ) exit /b 

If you don't want it to create a folder for each zip, change Call :UnZipFile "C:\Temp\%%~na\" "c:\path\to\%%~nxa" to Call :UnZipFile "C:\Temp\" "c:\path\to\%%~nxa"

like image 137
Matt Williamson Avatar answered Sep 23 '22 23:09

Matt Williamson