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

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

  • Comments 1
  • Likes
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.)

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hello Scripting Guys,

    As usual I am a day late and a dollar short.

    When I found this article I thought I had exactly what I wanted.

    I would like to store the ScriptName: Class.Script.vbs in a property for

    later use.

    When I run RestorePoint.Create.wsf I expected to see Class.Script.vbs in my MsgBox

    because I am doing it from within the Class.Script.vbs.

    What I am seeing is RestorePoint.Create.wsf.

    Is there a way to get the name of .vbs script.

    I am including the two files that I am using for test.

    Thanks,

    Bob Raney

    P.S. I know that its probably time for another vacation, but it's only been seven years.

    The .wsf script

    '----------------

    <job id = "RestorePoint.Create.wsf">

    <script language = "VBScript" Src = ".Include/Class.Script.vbs"/>

    <script language = "VBScript" Src = ".Include/Constants.vbs"/>

    <script language = "VBScript" Src = ".Include/Utility.vbs"/>

    <script language = "VBScript" Src = "Development/RestorePoint.Create.vbs"/>

    </job>

    The .vbs Script

    '-----------------

    '///////////////////////////////////////////////////////////////////////////////

    '/  Class.Script.vbs  

    '///////////////////////////////////////////////////////////////////////////////

    Option Explicit

    '///////////////////////////////////////////////////////////////////////////////

    '/  01-0000 2013/09/04; Initial Release; VBScript Version 5.8 Compliant.

    '///////////////////////////////////////////////////////////////////////////////

    Class Script_Object

    '------------------    

       Private MyTitle, _

               MyCopyright, _

               MyVersion

       Private Sub Class_Initialize()

               MyTitle          = "Class Script_Object"

               MyCopyright      = "2013"

               MyVersion        = "01-0000"

       End Sub

       Private Sub Class_Terminate()

       End Sub

       Public Property Get Title

              Title = MyTitle    

       End Property              

       Public Property Get Copyright

                Copyright = MyCopyright    

       End Property              

       Public Property Get Version

              Version = MyVersion    

       End Property

       Public Property Let Title(StrTitle)

              MyTitle = StrTitle

       End Property

       Public Property Let Copyright(StrYear)

              MyCopyright = StrYear    

       End Property

       Public Property Let Version(StrVersion)

              MyVersion = StrVersion    

       End Property

    End Class

    '///////////////////////////////////////////////////////////////////////////////

    Public Script

    '///////////////////////////////////////////////////////////////////////////////

    Private Sub Initialize()

    '-----------------------

    dim s

       Set Script = New Script_Object

       s = WScript.ScriptName

       msgbox s

    End Sub

    '///////////////////////////////////////////////////////////////////////////////

    Initialize

    '///////////////////////////////////////////////////////////////////////////////