Quick Hits: VBScript / WSH Script How to Reference Current Directory

By | January 1, 2011
Share

Vbscript does not include a variable populated with the current directory in which the script is called.  They do however include two other variables that give us the ability to dynamically grab this string:

Wscript.ScriptFullName = Full Path and Name of script ( ie. c:\Folder\SubFolder\Script.vbs )

Wscript.ScriptName = Name of Script ( ie. Script.vbs )

Using the Len built-in function, we can cut the name from the end of the “ScriptFullName” script to acheive our goal:

scriptPath = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) – Len(WScript.ScriptName))

Note: Output includes “\”  after folder name ( ie. c:\Folder\SubFolder\ )

In MDT ( Microsoft Deployment Toolkit ), the ZTIUTility.vbs script includes this functionality.  As long as you include this script in your custom .WSF, then you can use the “sScriptDir” variable.  That is populated via the “FileSystemObject”.

Set oFSO = CreateObject(“Scripting.FileSystemObject”)

sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)

Leave a Reply

Your email address will not be published. Required fields are marked *

*