gpt4 book ai didi

javascript - Windows脚本调用其他命令?

转载 作者:可可西里 更新时间:2023-11-01 09:56:03 27 4
gpt4 key购买 nike

我正在尝试使用 cscript 编写 Windows shell 脚本和 Javascript .我的想法是采用一个非常长的 Python 命令,我已经厌倦了一遍又一遍地在命令行中输入它。我想做的是编写一个更短的脚本,将整个 Python 命令写入 Windows 脚本,然后只调用 Windows 脚本,这样输入起来会少很多。我只是不知道如果有意义的话,我将如何调用“命令中的命令”。

这可能是一件容易的事情,但我是这方面的新手所以请多多包涵!

想法:

示例原始命令:python do <something really complicated with a long filepath>

Windows 脚本:cscript easycommand

 <package id = "easycommand">
<job id = "main" >
<script type="text/javascript">
// WHAT GOES HERE TO CALL python do <something really complicated>
WScript.Echo("Success!");
</script>
</job>
</package>

感谢您的帮助!

最佳答案

这是我用的

function logMessage(msg) {
if (typeof wantLogging != "undefined" && wantLogging) {
WScript.Echo(msg);
}
}

// http://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx
var windowStyle = {
hidden : 0,
minimized : 1,
maximized : 2
};

// http://msdn.microsoft.com/en-us/library/a72y2t1c(v=VS.85).aspx
var specialFolders = {
windowsFolder : 0,
systemFolder : 1,
temporaryFolder : 2
};

function runShellCmd(command, deleteOutput) {
deleteOutput = deleteOutput || false;
logMessage("RunAppCmd("+command+") ENTER");
var shell = new ActiveXObject("WScript.Shell"),
fso = new ActiveXObject("Scripting.FileSystemObject"),
tmpdir = fso.GetSpecialFolder(specialFolders.temporaryFolder),
tmpFileName = fso.BuildPath(tmpdir, fso.GetTempName()),
rc;

logMessage("shell.Run("+command+")");

// use cmd.exe to redirect the output
rc = shell.Run("%comspec% /c " + command + "> " + tmpFileName,
windowStyle.Hidden, true);
logMessage("shell.Run rc = " + rc);

if (deleteOutput) {
fso.DeleteFile(tmpFileName);
}
return {
rc : rc,
outputfile : (deleteOutput) ? null : tmpFileName
};
}

下面是一个示例,说明如何使用上面的方法使用 Appcmd.exe 列出 IIS 中定义的站点:

var
fso = new ActiveXObject("Scripting.FileSystemObject"),
windir = fso.GetSpecialFolder(specialFolders.WindowsFolder),
r = runShellCmd("%windir%\\system32\\inetsrv\\appcmd.exe list sites");
if (r.rc !== 0) {
// 0x80004005 == E_FAIL
throw {error: "ApplicationException",
message: "shell.run returned nonzero rc ("+r.rc+")",
code: 0x80004005};
}

// results are in r.outputfile

var
textStream = fso.OpenTextFile(r.outputfile, OpenMode.ForReading),
sites = [], item,
re = new RegExp('^SITE "([^"]+)" \\((.+)\\) *$'),
parseOneLine = function(oneLine) {
// each line is like this: APP "kjsksj" (dkjsdkjd)
var tokens = re.exec(oneLine), parts;

if (tokens === null) {
return null;
}
// return the object describing the website
return {
name : tokens[1]
};
};

// Read from the file and parse the results.
while (!textStream.AtEndOfStream) {
item = parseOneLine(textStream.ReadLine()); // you create this...
logMessage(" site: " + item.name);
sites.push(item);
}
textStream.Close();
fso.DeleteFile(r.outputfile);

关于javascript - Windows脚本调用其他命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11236506/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com