
Question:
I have a file Nirmal.zip in a E drive Suppose I extract the basename of the file alone "Nirmal" and created a folder out of it. Now there exists a folder named Nirmal and zip file named Nirmal.zip I need to extract the contents of Nirmal.zip and put it into newly created Nirmal Folder.
How to do the above using batch scripting
Answer1:Windows doesn't include unzip.exe
or any other similar sort of console executable for unzipping files. You can script it using Shell.Application
with <a href="https://gist.github.com/889769" rel="nofollow">JScript</a> or <a href="http://www.robvanderwoude.com/vbstech_files_zip.php" rel="nofollow">VBscript</a>, or even <a href="http://www.howtogeek.com/tips/how-to-extract-zip-files-using-powershell/" rel="nofollow">PowerShell</a> if you wish.
In the spirit of thoroughness, here's a Windows batch / JScript hybrid script that does what you ask:
@if (@a==@b) @end /*
:: unzip.bat
:: usage: unzip.bat zipfile.zip
:: extracts zipfile.zip to .\zipfile\
:: begin batch portion
@echo off
setlocal
if "%~1"=="" (
echo Usage: %~nx0 filename.zip
goto :EOF
)
cscript /nologo /e:jscript "%~f0" "%~f1"
echo(
echo Unzipping complete.
goto :EOF
:: end batch portion
:: begin JScript portion */
// https://gist.github.com/889769
// slightly modified by rojo for http://stackoverflow.com/a/27049936/1683264
function unzip(zipfile, unzipdir) {
var fso = new ActiveXObject('Scripting.FileSystemObject'),
shell = new ActiveXObject('Shell.Application'),
dst, zip;
if (!unzipdir) unzipdir = '.';
if (!fso.FolderExists(unzipdir)) fso.CreateFolder(unzipdir);
dst = shell.NameSpace(fso.getFolder(unzipdir).Path);
zip = shell.NameSpace(fso.getFile(zipfile).Path);
for (var i=0; i<zip.Items().Count; i++) {
try {
if (fso.FileExists(zipfile)) {
WSH.Stdout.Write('Unzipping ' + zip.Items().Item(i) + '... ');
dst.CopyHere(zip.Items().Item(i), 4 + 16);
WSH.Echo('Done.');
}
}
catch(e) {
WSH.Echo('Failed: ' + e);
}
}
}
var zipfile = WSH.Arguments(0),
dest = zipfile.replace(/\.\w+$/, '\\');
unzip(zipfile, dest);