VBScript - To Check your Versions - 检查你的运行版本

本文介绍了一个简单的VBScript脚本来检查Windows Scripting Host (WSH) 和VBScript版本号,包括如何获取版本信息和构建版本号。通过实践示例,展示了如何使用脚本确保代码兼容性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction to Checking Version Numbers

There are occasions when you need to investigate the VBScript version number. For example, if you have old clients with version 2.x., VBScript commands may not work. To give a specific example you may want a script to pause, in which case you could use WScript.Sleep. The problem is that this .Sleep property was only added in WSH version 5.1. If you are sure all your clients are XP, then no problem, but if the scripts are going to run on Windows 9x machines you may wish to check the version with a simple script.

The first bonus of checking version numbers is that investigation reveals just how many components are involved in a VBscript, for instance, Windows Scripting Host (WSH) and also VBScript itself.

Topics for Checking Version Numbers

Example 1 - VBScript to Check the WSH Version

VBScripts need a shell called Windows Scripting Host (WSH). Examples 1 and 2 are designed to extract the version information about WSH. (In Example 3 we switch to the VBScript itself.)

Prerequisites

This is a script that will execute equally well on a Windows server or an XP machine.

Instructions for Checking WSH Version

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Save the file with a .vbs extension, for example, WSH.vbs
  3. Double click WSH.vbsand read the version in the message box.

Sample VBScript to Check the WSH Version

' WSH.vbs
' Sample VBScript to check WSH Version
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - September 2005
' --------------------------------------------------------------------
On Error Resume Next
WScript.Echo "WSH Version: " & WScript.Version
WScript.Quit

VBScript Tutorial - Learning Points

Note 1: I am expecting the script to return a WScript version of 5.6

Note 2: Here we have a mixture of literals: "WSH Version: " and variables: WScript.Version. Paying attention to detail pays off. In this instance, notice how the space before the closing quotes makes the output easier to understand.

Note 3: Time and time ampersand (&) is your friend in joining VBScript components. I have to confess that I am often concentrating so hard on the more difficult commands that I forget the ampersand. When I make this sin of omission, WSH brings me up sharply with a message: Code 800A0401 - Expected end of statement.

<!--webbot bot="Include" U-Include="../../ThemesGuy/google_bannermiddle2a.htm" TAG="BODY" startspan -->

Example 2 - VBScript to Check the WSH Version and Build Number

Script to Display WSH and File Information

' WSHname.vbs
' Sample VBScript to check WSH Version
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.3 - September 2005
' --------------------------------------------------------------------
On Error Resume Next
WScript.Echo "WSH Version: " & WScript.Version & " " & WScript.BuildVersion _
& vbcr & "File name: " & WScript.ScriptName
WScript.Quit


VBScript Tutorial - Learning Points

Note 1: Most of the time, I only use WScript.Echo and perhaps WScript.Quit, I was surprised to discover that WScript has so many properties, for example .Version, .BuildVersion and even .ScriptName. From a teaching perspective, the message is that objects have properties, which we access with the syntax object.dot.property. Incidentally, my WScript build number was 8827.

Note 2: One potential problem with VBScript commands is that they are more than 80 characters long. Moreover, VBScript has no concept of word-wrap, so if you just continue the same instruction on the next line, VBScript will interpret your intention as two instructions and not one. Fortunately, there is a solution, the underscore (_). What underscore does is tell VBScript that the command has not yet finished, but to read the rest of the same command on the next line.

Note 3: Observe the command vbcr, this makes a carriage return in the actual output message box. It is purely cosmetic, but it often makes output easier to understand.

Example 3 - VBScript to Check the VBScript Version

As we will see our scripts require both VBScript itself and also WSH shell. This script concentrates on the VBScript executable. Incidentally, you can confirm the information by checking the information directly on cscript.exe in the Windows\System32 folder.

Please note that this script checks VBScript and is different from Example 1 and 2 which report version numbers for WSH. You will probably notice how ScriptEngineMajorVersion and ScriptEngineMinorVersion replace WScript.Version.

Sample Script to Check the VBScript Version

' VBScript.vbs
' Sample Script to check VBScript Version
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.1 - September 2005
' --------------------------------------------------------------------
WScript.Echo "VBScript Version: " & ScriptEngineMajorVersion _
& "." & ScriptEngineMinorVersion
WScript.Quit

VBScript Tutorial - Learning Points

Note 1: Unlike example 1 this script needs to build the version number from two numbers, and artificially concatenate a dot in between.

Challenge:

These three scripts are so short you could easily bolt them all together - could not you? This would mimic real life situations where you build scripts in stages, get them working then bolt them together to create the production script.

Summary of Checking Version Numbers

From a practical point of view, it may be necessary to check the version of VBScript or WSH. From a pure scripting point of view there are parallels with English. When composing ordinary English, many of us - me included, are unsure when to use a semi-colon; a dash is even harder to master. Syntax is equally important in VBScript, therefore take a minute or two to master the underscore (_) and vbcr.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值