windows VBScript、JScript、Wscript……天哪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1511301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:13:48  来源:igfitidea点击:

VBScript, JScript, Wscript ... oh my

windowsbatch-filevbscriptscriptingjscript

提问by Sukotto

I need to write some scripts for WinXP to support some of the analysts here at Big Financial Corp. Please help me decide which type of windows scripting best fits my needs.

我需要为 WinXP 编写一些脚本来支持 Big Financial Corp 的一些分析师。请帮助我决定哪种类型的 Windows 脚本最适合我的需要。

My needs seem pretty simple (to me anyway)

我的需求似乎很简单(无论如何对我来说)

  1. run on WinXP Pro SP2 (version 2002)
  2. not require my users to install anything (so Powershell is out. Likewise Perl, Python, and other common suggestions for these types of questions on stackoverflow)
  3. written in a non-compiled language (so users have a chance to modify them in the future)
  4. reasonably complete language features (especially date/time manipulation functions. I would like to also have modern concepts like subroutines, recursion, etc)
  5. ability to launch and control other programs (at the commandline)
  1. 在 WinXP Pro SP2(2002 版)上运行
  2. 不需要我的用户安装任何东西(所以 Powershell 已经过时了。对于 stackoverflow 上的这些类型的问题,同样是 Perl、Python 和其他常见的建议)
  3. 用非编译语言编写(因此用户将来有机会修改它们)
  4. 相当完整的语言功能(尤其是日期/时间操作函数。我还想拥有现代概念,如子程序、递归等)
  5. 启动和控制其他程序的能力(在命令行中)

From my hurried review of my options, it looks like my choices are

从我对我的选择的匆忙回顾来看,我的选择似乎是

  1. VBScript
  2. WScript
  3. JScript
  1. 脚本
  2. 脚本
  3. 脚本

I don't have time to learn or do an in-depth review of these (or whatever else a standard install of WinXP has available). I have a pretty urgent need to pick on and hack something together as quickly as possible.

我没有时间学习或深入研究这些(或任何其他可用的 WinXP 标准安装)。我有一个非常迫切的需要,尽快挑选和破解一些东西。

(Current crisis is the need to run a given application, passing several date parameters).

(当前的危机是需要运行给定的应用程序,传递几个日期参数)。

Once the current crisis is over, there will be more requests like this.

一旦当前的危机结束,就会有更多这样的要求。

Help me Obi WanStackoverflow... you're my only hope.

帮助我Obi WanStackoverflow...你是我唯一的希望。

[edit] My current skill set includes Perl, Javascript, and Java so I'm most comfortable using something similar to these

[编辑] 我目前的技能包括 Perl、Javascript 和 Java,所以我最喜欢使用类似的东西

[edit] ok. I'll try writing a WSH file in JScript. Thanks everyone... I'll let you know how it goes (and figure out accepting an answer) once things settle down around here a bit.

[编辑] 好的。我将尝试用 JScript 编写一个 WSH 文件。谢谢大家......一旦事情在这里稳定下来,我会告诉你事情的进展(并弄清楚接受答案)。

[edit] It all worked out in the end. Thanks for the quick responses folks. Here's what I gave my user

[编辑] 最后一切都解决了。感谢人们的快速回复。这是我给我的用户的

<job id="main">
    <script language="JScript">
// ----- Do not change anything above this line ----- //

var template = "c:\path\to\program -##PARAM## --start ##date1## --end ##date2## --output F:\path\to\whereever\ouput_file_##date1##.mdb";

// Handle dates
// first, figure out what they should be
dt = new Date();
var date1 = stringFromDate(dt, 1);
var date2 = stringFromDate(dt, 2);

// then insert them into the template
template = template.replace(new RegExp("##date1##", "g"), date1);
template = template.replace(new RegExp("##date2##", "g"), date2);

// This application needs to run twice, the only difference is a single parameter
var params = ["r", "i"]; // here are the params.

// set up a shell object to run the command for us
var shellObj = new ActiveXObject("WScript.Shell");

// now run the program once for each of the above parameters
for ( var index in params )
{
    var runString = template; // set up the string we'll pass to the wondows console
    runString = runString.replace(new RegExp("##PARAM##", "g"), params[index]); // replace the parameter
    WScript.Echo(runString);

    var execObj = shellObj.Exec( runString ); 
    while( execObj.Status == 0 )
    {
        WScript.Sleep(1000); //time in milliseconds
    }
    WScript.Echo("Finished with status: " + execObj.Status + "\n");
}


// ----- supporting functions ----- //

// Given a date, return a string of that date in the format yyyy-m-d
// If given an offset, it first adjusts the date by that number of days
function stringFromDate(dateObj, offsetDays){
    if (typeof(offsetDays) == "undefined"){
        offsetDays = 0;
    }
    dateObj.setDate( dateObj.getDate() + offsetDays );

    var s = dateObj.getYear() + "-";     //Year
    s += (dateObj.getMonth() + 1) + "-"; //Month (zero-based)
    s += dateObj.getDate();              //Day

    return(s);
}

// ----- Do not change anything below this line ----- //
    </script>
</job>

Clearly it could be better... but it got the job done and is easy enough for my user to understand and extend himself.

显然它可以更好......但它完成了工作并且很容易让我的用户理解和扩展自己。

回答by snicker

These are all technically the same thing with different syntax. Actually WScript/CScript is the engine, VBScript and JScript are the languages.

这些在技术上都是相同的东西,但语法不同。实际上 WScript/CScript 是引擎,VBScript 和 JScript 是语言。

Personal opinion only follows: My personal recommendation is JScript because it reminds me more of a real programming language, and makes me want to punch myself in the face lessoften than VBScript. And given your familiarity with javascript, your best bet is JScript.

个人认为只有如下:我个人的建议是JScript的,因为它让我想起更多的是真正的编程语言,并让我想冲自己的脸往往比VBScript中。鉴于您对 javascript 的熟悉程度,您最好的选择是 JScript。

Going into a bit more detail about the difference between WScript and CScript as others have: these are your execution platforms for your scripts for the Windows Script Host. They are essentially the same thing, whereas WScript is more GUI oriented, and CScript is more console oriented. If you start the script with CScript, you will see a console window, but you still have access to GUI functionality, whereas if you start with WScript, there is no console window, and many of the default output methods display as windowed objects rather than a line in the console.

像其他人一样更详细地了解 WScript 和 CScript 之间的区别:这些是用于 Windows 脚本宿主的脚本的执行平台。它们本质上是一样的,而 WScript 更面向 GUI,而 CScript 更面向控制台。如果您使用 CScript 启动脚本,您将看到一个控制台窗口,但您仍然可以访问 GUI 功能,而如果您使用 WScript 启动,则没有控制台窗口,并且许多默认输出方法显示为窗口对象而不是控制台中的一行。

回答by Shog9

If you like JavaScript, you'll probably be ok with JScript. It's a decent language, and certainly more suitable for complex scripts than VBScript.

如果您喜欢 JavaScript,那么您可能会喜欢JScript。它是一种不错的语言,当然比 VBScript 更适合复杂的脚本。

However, Microsoft1hatesJavaScript, so you'll encounter some APIs that are trivial to use with VBScript but painful to access using JScript. Consider yourself warned...

但是,Microsoft 1讨厌JavaScript,因此您会遇到一些 API,这些 API 与 VBScript 一起使用很简单,但使用 JScript 访问却很痛苦。考虑一下自己被警告...

As snicker notes, WScript is the engine that drives both.

正如士力架所指出的,WScript 是驱动两者的引擎。

1 Anthropomorphization used to note general lack-luster support; not to be interpreted as evidence of any official policy.

1 Anthropomorphization 用于说明一般缺乏光泽的支持;不得被解释为任何官方政策的证据。

回答by system PAUSE

Use JScript. A key difference between using JScript with the WScript/cscript engine and writing JavaScript in the browser is that you don't have the browser security restrictions. You also have access to ActiveX/COM objects for manipulating applications, the registry, etc. In fact, you'll probably spend a lot more time reading up on those custom objects and interfaces than worrying about the language features. Of course, you get all the benefits of JavaScript dates, regex's, etc.

使用 JScript。将 JScript 与 WScript/cscript 引擎一起使用与在浏览器中编写 JavaScript 之间的主要区别在于您没有浏览器安全限制。您还可以访问 ActiveX/COM 对象来操作应用程序、注册表等。事实上,与担心语言特性相比,您可能会花更多的时间阅读这些自定义对象和接口。当然,您可以获得 JavaScript 日期、正则表达式等的所有好处。

A sample JScript app from MSDNmight help to get you started.

一个从MSDN示例JScript中的应用程序可以帮助您开始。

Unfortunately, most of Microsoft's sample scripts tend to be VBScript, but the syntax is pretty easy to follow, especially if you're just trying to pick out COM interface details.

不幸的是,大多数 Microsoft 的示例脚本往往是 VBScript,但其语法很容易遵循,尤其是当您只是想挑选 COM 接口详细信息时。

回答by Todd

To expand on the other's answers a bit, WScript and CScript are programs used to run scripts written in VBScript (Visual Basic like syntax) or JScript (JavaScript-like syntax). CScript runs scripts from a console window, so that the Echo command writes to the console, whereas WScript runs them without a window and the Echo command writes to a popup message box.

为了稍微扩展其他人的答案,WScript 和 CScript 是用于运行用 VBScript(类似 Visual Basic 的语法)或 JScript(类似 JavaScript 的语法)编写的脚本的程序。CScript 从控制台窗口运行脚本,以便 Echo 命令写入控制台,而 WScript 在没有窗口的情况下运行它们并且 Echo 命令写入弹出消息框。

You can write WSH (Windows Scripting Host) and WSC (Windows Scripting Component) scripts that use both VBScript and JScript by combining them in an XML-based wrapper, if you need to merge pre-existing code in the two languages.

如果您需要合并两种语言中预先存在的代码,您可以编写同时使用 VBScript 和 JScript 的 WSH(Windows 脚本宿主)和 WSC(Windows 脚本组件)脚本,方法是将它们组合在基于 XML 的包装器中。

You can also write HTA scripts, which stands for "HyperText Application". These are script code in an HTML file with the HTA extension that runs in Internet Explorer, which allows you to provide an interface using HTML controls, but also have complete access to your system because the run locally.

您还可以编写 HTA 脚本,它代表“超文本应用程序”。这些是在 Internet Explorer 中运行的带有 HTA 扩展名的 HTML 文件中的脚本代码,它允许您提供使用 HTML 控件的界面,但也可以完全访问您的系统,因为在本地运行。

All of these are based on the Windows Scripting Host and Active Scripting technologies which have been included with all Windows computers since Windows 98. Along with fairly powerful base languages, they also give you access to WMI for extensive system and network information and management, and COM capability for automating Word, Excel etc. Also you can use ADO and ADOX to create and work with Access MDB files even if Access is not installed.

所有这些都基于 Windows Scripting Host 和 Active Scripting 技术,自 Windows 98 以来,所有 Windows 计算机都包含这些技术。除了相当强大的基础语言外,它们还使您可以访问 WMI 以获取广泛的系统和网络信息以及管理,以及用于自动化 Word、Excel 等的 COM 功能。即使未安装 Access,您也可以使用 ADO 和 ADOX 创建和使用 Access MDB 文件。

回答by Daniel Earwicker

Although JScript is a much less horrible language than VB Script, the problem is that VB Script has a more complete library of helpful functions built into it for things like date and number formatting. So it's not actually as easy a choice as it first appears, unless you are able to write and install your own library of helper objects to use with JScript.

尽管 JScript 是一种远没有 VB Script 可怕的语言,但问题是 VB Script 有一个更完整的内置有用函数库,用于日期和数字格式等。因此,它实际上并不像最初出现的那样容易,除非您能够编写和安装自己的辅助对象库以与 JScript 一起使用。

Some details here.

一些细节在这里

回答by RedFilter

My choice would be WSH using JScript. You could use VBScript, but why, when JScript is available.

我的选择是使用 JScript 的 WSH。您可以使用 VBScript,但是为什么,当 JScript 可用时。

Here is a referencefor Windows Script Host.

这是Windows 脚本宿主的参考

回答by Joel Coehoorn

Don't forget CScript. And be careful here, because the windows scripting host is often disabled by group policy at large companies. If that's the case, the only option that fits all your criteria is (shudder) batch.

不要忘记 CScript。在这里要小心,因为 Windows 脚本主机通常被大公司的组策略禁用。如果是这种情况,唯一符合您所有标准的选项是(不寒而栗)批次。

If noneof those work out for you, your best option is probably a compiled program where you distribute the source with the program.

如果这些都不适合您,那么您最好的选择可能是编译程序,您可以在其中随程序一起分发源代码。