使用批处理脚本解压缩文件 [英] Unzip a file using batch scripting

查看:659
本文介绍了使用批处理脚本解压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在E盘中有一个文件Nirmal.zip 假设我仅提取文件"Nirmal"的基本名称,并从中创建一个文件夹. 现在,存在一个名为Nirmal的文件夹和一个名为Nirmal.zip的zip文件. 我需要提取Nirmal.zip的内容,并将其放入新创建的Nirmal文件夹中.

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

推荐答案

Windows不包含用于解压缩文件的unzip.exe或任何其他类似的控制台可执行文件.您可以使用Shell.Application JScript

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 JScript or VBscript, or even PowerShell if you wish.

本着周全的精神,这是一个Windows批处理/JScript混合脚本,可以满足您的要求:

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);

这篇关于使用批处理脚本解压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆