运行 JScript 中的 cmd 命令

Run cmd commands in JScript

我有一个 cmd 命令需要在 JScript 中执行。所以,我正在尝试下一个代码序列:

var WshShell = new ActiveXObject("WScript.Shell");
sevenZip = "C:\Program Files\7-Zip\7z.exe";
WshShell.Run("cmd.exe /c '"+sevenZip+" e "+File+" -p"+Pass+"'");

但是当我启动脚本时,cmd window 打开,然后关闭,但似乎没有执行该命令。我做错了什么?

我建议改用 Shell.Aplication。这是代码的样子:

var objShell = new ActiveXObject("Shell.Application");
sevenZip = "C:\Program Files\7-Zip\7z.exe";
executableCommand = "x " + zipFile + " -p" + zipPass + " -o" + Temp;
objShell.ShellExecute("7z.exe", executableCommand, sevenZip , "open", 0);

chcp 1251,866 - 将其更改为您的区域参数

var htmlfile=new ActiveXObject('htmlfile').parentWindow;
var alert=function(s){htmlfile.alert(s)};
var echoIt='Hi'
//...solve the problem of doubling percentages in variables during transmission by introducing the parameter /v!
var cmdCode=[
    'for /F "tokens=1-6 delims=:., " %A In ("!date! !time!") Do (Echo %A.%B.%C %D:%E:%F)',
    'set var='+echoIt,
    'echo %var%',
    'echo !var!'
];//'setlocal disableDelayedExpansion' parameter is useless becose /v
var std, arr=[];
var WshRunning=0,WshFinished=1,WshFailed=2;var i=0;tryCount=0,output=[],errors=[];  
with (new ActiveXObject('WScript.Shell')) {
    std=Exec("cmd /v /q /k echo off"); //Turning on the /v option last, switches the output here in its entirety, including C:\...\notepad>
    std.StdIn.WriteLine("chcp 1251>nul");
    for(var i=0;i<cmdCode.length;i++) {
        std.StdIn.WriteLine(cmdCode[i]);
    };
    std.StdIn.WriteLine("chcp 866>nul");
    std.StdIn.WriteLine("exit");
    do{
        if (std.Status==WshFailed){
            errors.push('Error in string '+i+' :\n   '+std.StdErr.ReadLine());
            tryCount++
        }
        else if(std.Status==WshRunning){
            output.push(std.StdOut.ReadLine());
            tryCount=0;
            WScript.Echo('Running ...')
        }
        else if(std.Status==WshFinished){
            var last=std.StdOut.ReadLine();
            if(last.length>0){output.push(last)};last=undefined;
            tryCount=21;
            WScript.Echo('Finished ...')
        }i++;
    }while(tryCount<21);    
}
WScript.Echo('')
if (output.length>0){for(var i=0;i<output.length;i++) {WScript.Echo(output[i])}}
if (errors.length>0){alert(errors)}
var x=WScript.StdIn.ReadLine(); 

var std, arr = [];
var oWsh = new ActiveXObject("WScript.Shell");
with (new ActiveXObject('WScript.Shell')) {
    std = Exec("cmd /q /k chcp 1251>nul");
    with (std.StdIn){
        WriteLine("dir "+ oWsh.SpecialFolders("Desktop"));
        WriteLine("exit");
    }
    arr = std.StdOut.ReadAll().split('\n'); //чтение данных
}
for (var i = 0; i < arr.length; i++) {
    WScript.echo(i+' '+arr[i]);
}   
var x = WScript.StdIn.ReadLine();