Monday, May 11, 2009

Professional Cat-Skinning with a Butter Knife

Yes, the dumb article titles just never stop coming.  Just be glad I don’t drink Red Bull as much as I do standard version coffee.

Someone commented on the ScriptPath() function I posted earlier and how I was deriving the path of the script by subtracting one string from another.  This this case, I use the Replace() function to subtract the ScriptName of the script file from the “ScriptFullName” property, which includes the path and filename together.

p = Replace(Wscript.ScriptFullName, Wscript.ScriptName, "")

Are there other ways to do this?  Sure.  One suggestion I received was to use the GetBaseName() function provided by the Scripting.FileSystemObject API. 

Set fso = CreateObject("Scripting.FileSystemObject")
p = fso.GetBaseName(Wscript.ScriptFullName)
Set fso = Nothing

The reason I didn’t do it that way was that it requires instantiating the FileSystemObject just to get one small string value and then throw the object away.  That felt like buying a box of grapes, eating one single grape and throwing the rest into a trashcan.   I hate wasting things.  Another reason is that if you just look at the two methods, the first one is a single line of code.  An old friend of mine used to refer to such practices as "elegant coding".  I like the sound of that also.  So, instead, I opted to do it the sneaky-slick-tricky-string-manipulation way.

Post a Comment