在 Windows 上选择哪个:VBScript、JScript、Wscript

发布于 2024-08-06 17:18:17 字数 2817 浏览 2 评论 0原文

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

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

  1. 在 WinXP Pro SP2(版本 2002)上运行
  2. 不需要我的用户安装任何东西(所以 PowerShell 已经淘汰了。同样 Perl、Python 和 Stack Overflow 上针对此类问题的其他常见建议)
  3. 用非编译语言编写(以便用户将来有机会修改它们)
  4. 相当完整的语言功能(特别是日期/时间操作功能。我还希望拥有子例程、递归等现代概念)
  5. 启动能力并控制其他程序(在命令行)

从我对选项的匆忙审查来看,我的选择似乎是

  1. VBScript
  2. WScript
  3. JScript

我没有时间学习或深入审查这些(或其他任何标准) WinXP 的安装可用)。我需要尽快挑选一些东西并将其组合在一起。

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

一旦当前的危机结束,类似的请求将会更多。

编辑

我当前的技能包括 Perl、JavaScript 和 Java,所以我最喜欢使用与这些类似的东西

编辑

OK。我将尝试用 JScript 编写 WSH 文件。一旦事情稳定下来,我会让你知道事情进展如何(并弄清楚接受答案)。

编辑

这一切最终都解决了。感谢大家的快速回复。这是我给我的用户的:

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

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

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

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 Stack Overflow)
  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 command line)

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

  1. VBScript
  2. WScript
  3. JScript

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 need to pick on and hack something together as quickly as possible.

(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.

Edit

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

Edit

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

落叶缤纷 2024-08-13 17:18:17

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

个人意见仅如下:我个人推荐 JScript,因为它让我更多地想起一种真正的编程语言,并且比 VBScript 更让我想打自己的脸更少。鉴于您对 javascript 的熟悉程度,您最好的选择是 JScript。

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

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

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 less often than VBScript. And given your familiarity with javascript, your best bet is 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.

花想c 2024-08-13 17:18:17

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

然而,Microsoft1 讨厌 JavaScript,因此您会遇到一些 API,这些 API 对于 VBScript 来说很简单,但使用 JScript 来访问却很痛苦。考虑一下自己被警告过...

正如窃笑者所说,WScript 是引擎这推动了两者。

1 拟人化用于指出一般缺乏光泽的支持;不得被解释为任何官方政策的证据。

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.

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

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

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

怀念你的温柔 2024-08-13 17:18:17

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

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

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

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

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.

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.

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.

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.

漫漫岁月 2024-08-13 17:18:17

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

此处有一些详细信息

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.

Some details here.

多彩岁月 2024-08-13 17:18:17

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

来自 MSDN 的示例 JScript 应用程序可能会帮助您入门。

不幸的是,大多数 Microsoft 的示例脚本往往是 VBScript,但语法非常容易遵循,特别是如果您只是想找出 COM 接口详细信息。

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.

A sample JScript app from MSDN might help to get you started.

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.

你是我的挚爱i 2024-08-13 17:18:17

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

以下是 Windows 脚本宿主的参考

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

Here is a reference for Windows Script Host.

因为看清所以看轻 2024-08-13 17:18:17

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

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

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.

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

疯了 2024-08-13 17:18:17

截至 2023 年 10 月,VBScript 已悄然被弃用,因此您应该避免将其用于想要长期使用的新脚本。根据 https://learn.microsoft.com/en- us/windows/whats-new/deprecated-features

VBScript 已被弃用。在 Windows 的未来版本中,VBScript 将在从操作系统中删除之前作为一项按需功能提供。

https://learn.microsoft.com /en-us/windows/whats-new/deprecated-features-resources#vbscript

VBScript 将作为一项按需功能提供,然后在未来的 Windows 版本中停用。最初,将预安装 VBScript 按需功能,以便您在为 VBScript 退役做准备时可以不间断地使用。

如果您在旧版本的 Windows 上使用 VBScript,或者在当前的 Windows 11 上使用它但只是一次性脚本,则不必担心,因为它目前仍然可用,但如果您是为将来编写脚本,请考虑使用更现代的脚本语言,例如 PowerShell 或 Python(尽管我会怀念 VBScript 中与 COM 的轻松集成,而不是 PowerShell 中相同任务的冗长)。

As of October 2023, VBScript has quietly been deprecated, so you should avoid using it for new scripts that you want to work for a very long time. According to https://learn.microsoft.com/en-us/windows/whats-new/deprecated-features :

VBScript is being deprecated. In future releases of Windows, VBScript will be available as a feature on demand before its removal from the operating system.

and https://learn.microsoft.com/en-us/windows/whats-new/deprecated-features-resources#vbscript :

VBScript will be available as a feature on demand before being retired in future Windows releases. Initially, the VBScript feature on demand will be preinstalled to allow for uninterrupted use while you prepare for the retirement of VBScript.

If you're using VBScript on older versions of Windows or you're using it on the current Windows 11 but just for a one-off script, you don't have to worry, as it's still available for now, but if you are writing scripts for the future, consider using a more modern scripting language such as PowerShell or Python (although I will miss the easy integration with COM in VBScript as opposed to the verbosity of the same task in PowerShell).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文