{"id":66703,"date":"2006-08-11T07:33:00","date_gmt":"2006-08-11T07:33:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/11\/how-can-i-determine-the-name-of-a-script-while-that-script-is-running\/"},"modified":"2006-08-11T07:33:00","modified_gmt":"2006-08-11T07:33:00","slug":"how-can-i-determine-the-name-of-a-script-while-that-script-is-running","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-name-of-a-script-while-that-script-is-running\/","title":{"rendered":"How Can I Determine the Name of a Script While That Script is Running?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>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.<BR><BR>&#8212; TB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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\u2019s 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. <\/P>\n<P>True, but that\u2019s only at work. In addition to that, he also plans on doing absolutely nothing at home.<\/P>\n<P>Of course, this Scripting Guy is only taking one week off; he\u2019s 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.<\/P>\n<TABLE id=\"E4C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B><I>Editor\u2019s Note:<\/I><\/B><I> Before the mass hysteria begins: don\u2019t worry. The Scripting Guy left plenty of <\/I>Hey, Scripting Guy!<I> articles to cover his absence, the daily column <\/I>will<I> go on. You can relax knowing your faithful column will still be here each day, the only difference being the <\/I><I>Scripting E<\/I><I>ditor might have a little more fun next week than usual.<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>It\u2019s just a coincidence that today\u2019s question can be answered with only two lines of code:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Script name: &#8221; &amp; Wscript.ScriptName\nWscript.Echo &#8220;Script path: &#8221; &amp; Wscript.ScriptFullName\n<\/PRE>\n<P>Hey, it could have been worse: we could have answered this question using just <I>one<\/I> line of code. But that <I>really<\/I> 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\u2019s in addition to the line of code that returns the script name.<\/P>\n<P>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 <B>ScriptName<\/B> property returns the file name of the script; as the name doesn\u2019t necessarily imply, the <B>ScriptFullName<\/B> property returns the file path for the script.<\/P>\n<P>For example, suppose you\u2019re running the script C:\\Scripts\\Test.vbs. What do you suppose the ScriptName and ScriptFullName properties will return?<\/P>\n<P>You got it:<\/P><PRE class=\"codeSample\">Script name: test.vbs\nScript path: C:\\scripts\\test.vbs\n<\/PRE>\n<P>You\u2019re right: even for a Scripting Guy it\u2019s a bit early to sneak out, isn\u2019t it? Well, we could show you some slides from last year\u2019s vacation; in fact, we could even \u2013 oh. OK. How about this? Here\u2019s 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:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Build number: &#8221; &amp; _\n    ScriptEngineMajorVersion &amp; &#8220;.&#8221; &amp; ScriptEngineMinorVersion &amp; &#8220;.&#8221; &amp; ScriptEngineBuildVersion\n<\/PRE>\n<P>As you can see, in this little script we simply combine three VBScript functions: <B>ScriptEngineMajorVersion<\/B>, <B>ScriptEngineMinorVersion<\/B>, and <B>ScriptEngineBuildVersion<\/B>. Put them all together and they spell something similar to this:<\/P><PRE class=\"codeSample\">Build number: 5.6.8820\n<\/PRE>\n<P>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:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Version: &#8221; &amp; Wscript.Version\n<\/PRE>\n<P>OK, that should just about \u2013 oh. Seems the Scripting Editor <I>still<\/I> doesn\u2019t believe we\u2019ve done enough work for this column. What\u2019s that? Is the Scripting Editor one of the reasons we\u2019re so anxious to get away? Well, as a matter of fact \u2013 uh, no, no. Of course not \u2026.<\/P>\n<P><B><I>Editor\u2019s Note:<\/I><\/B><I> It\u2019s probably pretty obvious who\u2019s <\/I>really<I> getting the vacation here.<\/I><\/P>\n<P>Tell you what: how \u2018bout we show you some code that lets a script know whether or not it\u2019s running under WScript or CScript? (An important consideration: not only do you potentially face the prospect of clicking <B>OK<\/B> in hundreds of message boxes if certain scripts are run under WScript, but there are a few things \u2013 such as StdIn and StdOut \u2013 that are available only if you are running under CScript.) Scripting veterans have seen this before, but newcomers might find it handy:<\/P><PRE class=\"codeSample\">strScriptHost = LCase(Wscript.FullName)<\/p>\n<p>If Right(strScriptHost, 11) = &#8220;wscript.exe&#8221; Then\n    Wscript.Echo &#8220;This script is running under WScript.&#8221;\nElse\n    Wscript.Echo &#8220;This script is running under CScript.&#8221;\nEnd If\n<\/PRE>\n<P>How does this code help us determine which script host we\u2019re running under? Well, the <B>FullName<\/B> 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 <I>wscript.exe<\/I> then we\u2019re running under WScript; otherwise we\u2019re running under CScript.<\/P>\n<P>With that in mind, we use the <B>LCase<\/B> function to convert the FullName to lowercase letters, and then store that value in the variable strScriptHost:<\/P><PRE class=\"codeSample\">strScriptHost = LCase(Wscript.FullName)\n<\/PRE>\n<P>From there we use the <B>Right<\/B> function to determine whether or not the last 11 characters in strScriptHost are equal to <I>wscript.exe<\/I>. If they are we echo back the fact that the script is running under WScript; otherwise we echo back the fact that we\u2019re running the script under CScript.<\/P>\n<TABLE id=\"EBG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. You say you\u2019re not familiar with the Right function? Boy, is this your lucky month: try to guess what the latest edition of <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/begin\/ss0806.mspx\"><B>Sesame Script<\/B><\/A> is all about?<\/P>\n<P>Um, no, that\u2019s not even close: it\u2019s about the Right, Left, and Mid functions. But that was a good guess \u2026 sort of.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>And now we <I>are<\/I> going to go. (It\u2019s either that or we start showing last year\u2019s vacation slides; take your pick.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.&#8212; TB Hey, TB. You know, the Scripting Guy who writes this column is in the office today, trying to wrap things up [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2,3,4,5],"class_list":["post-66703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>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.&#8212; TB Hey, TB. You know, the Scripting Guy who writes this column is in the office today, trying to wrap things up [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66703\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}