在JScript中运行cmd命令

时间:2018-10-02 14:50:52

标签: 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窗口会打开,然后关闭,但是该命令似乎没有执行。我在做什么错了?

2 个答案:

答案 0 :(得分:1)

我建议改为使用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);

答案 1 :(得分: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();
相关问题