August 11th, 2006

How Can I Determine the Name of a Script While That Script is Running?

Hey, Scripting Guy! Question

Hey, Scripting Guy! From within the script itself, how can I get the name of a running script? I need that in order to create a generic error handler for all my scripts.

— TB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TB. You know, the Scripting Guy who writes this column is in the office today, trying to wrap things up so he can start a well-deserved vacation. And no, he’s not going anywhere: after carting assorted mothers, aunts, sisters, and even a friend of the Scripting Son to Europe last year, this year he fully intends to spend his time doing absolutely nothing.

True, but that’s only at work. In addition to that, he also plans on doing absolutely nothing at home.

Of course, this Scripting Guy is only taking one week off; he’s not taking one week and one day off. Therefore, you can rest assured that he will put in a full day today, working tirelessly to help answer your scripting questions.

Editor’s Note: Before the mass hysteria begins: don’t worry. The Scripting Guy left plenty of Hey, Scripting Guy! articles to cover his absence, the daily column will go on. You can relax knowing your faithful column will still be here each day, the only difference being the Scripting Editor might have a little more fun next week than usual.

It’s just a coincidence that today’s question can be answered with only two lines of code:

Wscript.Echo “Script name: ” & Wscript.ScriptName
Wscript.Echo “Script path: ” & Wscript.ScriptFullName

Hey, it could have been worse: we could have answered this question using just one line of code. But that really looked like we were just trying to get the column done so we could sneak out early. Therefore, we tossed in a bonus line of code that retrieves the complete file path; that’s in addition to the line of code that returns the script name.

As it turns out, the Wscript object (the parent object for Windows Script Host) has a pair of properties that can return meta-information about a script while that script runs. As the name implies, the ScriptName property returns the file name of the script; as the name doesn’t necessarily imply, the ScriptFullName property returns the file path for the script.

For example, suppose you’re running the script C:\Scripts\Test.vbs. What do you suppose the ScriptName and ScriptFullName properties will return?

You got it:

Script name: test.vbs
Script path: C:\scripts\test.vbs

You’re right: even for a Scripting Guy it’s a bit early to sneak out, isn’t it? Well, we could show you some slides from last year’s vacation; in fact, we could even – oh. OK. How about this? Here’s a nice little chunk of code that can return the build number for VBScript; that can be useful in ensuring that your computers are all fully up-to-date with the latest patches and all:

Wscript.Echo “Build number: ” & _
    ScriptEngineMajorVersion & “.” & ScriptEngineMinorVersion & “.” & ScriptEngineBuildVersion

As you can see, in this little script we simply combine three VBScript functions: ScriptEngineMajorVersion, ScriptEngineMinorVersion, and ScriptEngineBuildVersion. Put them all together and they spell something similar to this:

Build number: 5.6.8820

If all you care about is whether your computers are running WSH 5.6 (as opposed to WSH 2.0) then this line of code is all you need:

Wscript.Echo “Version: ” & Wscript.Version

OK, that should just about – oh. Seems the Scripting Editor still doesn’t believe we’ve done enough work for this column. What’s that? Is the Scripting Editor one of the reasons we’re so anxious to get away? Well, as a matter of fact – uh, no, no. Of course not ….

Editor’s Note: It’s probably pretty obvious who’s really getting the vacation here.

Tell you what: how ‘bout we show you some code that lets a script know whether or not it’s running under WScript or CScript? (An important consideration: not only do you potentially face the prospect of clicking OK in hundreds of message boxes if certain scripts are run under WScript, but there are a few things – such as StdIn and StdOut – that are available only if you are running under CScript.) Scripting veterans have seen this before, but newcomers might find it handy:

strScriptHost = LCase(Wscript.FullName)

If Right(strScriptHost, 11) = “wscript.exe” Then Wscript.Echo “This script is running under WScript.” Else Wscript.Echo “This script is running under CScript.” End If

How does this code help us determine which script host we’re running under? Well, the FullName property returns the path to the executable file for the current script host; depending on where Windows has been installed that will usually be either C:\Windows\System32\Cscript.exe or C:\Windows\System32\Wscript.exe. If the last 11 characters in FullName are wscript.exe then we’re running under WScript; otherwise we’re running under CScript.

With that in mind, we use the LCase function to convert the FullName to lowercase letters, and then store that value in the variable strScriptHost:

strScriptHost = LCase(Wscript.FullName)

From there we use the Right function to determine whether or not the last 11 characters in strScriptHost are equal to wscript.exe. If they are we echo back the fact that the script is running under WScript; otherwise we echo back the fact that we’re running the script under CScript.

Note. You say you’re not familiar with the Right function? Boy, is this your lucky month: try to guess what the latest edition of Sesame Script is all about?

Um, no, that’s not even close: it’s about the Right, Left, and Mid functions. But that was a good guess … sort of.

And now we are going to go. (It’s either that or we start showing last year’s vacation slides; take your pick.)

Author

0 comments

Discussion are closed.