How to make sure that a script is run using cscript

01/22/09

Permalink 04:43:19 am, by dave Email , 213 words, 995 views   English (US)
Categories: Windows Scripting, Wsh Scripts

How to make sure that a script is run using cscript

Most of the scripts I write are intended to be called from the command window using cscript.exe. Something like.....( the nologo argument gets rid of the title block that cscript spits outs everytime it runs )

C:\scripts>cscript /nologo someScript.vbs

Recently I ran into a situation where a script was being called using wscript.exe instead and the script was not acting properly. After a little research, I came up with this solution:

[More:]

You need to figure out which interpreter is being used. You do this by looking at the fullname property of the wscript object.

'==========================================================
'== declare the local variables to be used
'==========================================================
dim scriptHost, tmpArr

'==========================================================
'== get the name of the interpreter being used to run the
'== script - have to parse it out of the full path that is
'== stored in the fullname property
'==========================================================
scriptHost = wscript.fullname
tmpArr = split( scriptHost, "\" )
scriptHost = tmpArr( ubound(tmpArr) )

if lcase( scriptHost ) <> "cscript.exe" then
    wscript.echo "This script must be executed using cscript.exe"
    wscript.quit
end if

I hope this post helped out. If it didn't, I am always looking for new scripts to add so submit a request for your question or need and I will see if I can answer it.

Dave



Did you like this post? If so, Share it!  del.icio.us digg reddit slashdot this article Facebook Twitter MySpace Email



Pingbacks:

No Pingbacks for this post yet...

This post has 11 feedbacks awaiting moderation...

Scripts

This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.

Search

Follow Me:

Misc

Who's Online?

  • Guest Users: 2

powered by b2evolution free blog software