Getting current directory in VBScript

CodeKeyer picture CodeKeyer · Apr 22, 2013 · Viewed 193.8k times · Source

I'm trying to get the current directory and use it to run an application no matter where the file is put and no matter how the path is changed

Dim fso: set fso = CreateObject("Scripting.FileSystemObject")
Dim CurrentDirectory
CurrentDirectory = fso.GetAbsolutePathName(".")
Dim Directory
Directory = CurrentDirectory\attribute.exe

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "Directory" & Chr(34), 0
Set WinScriptHost = Nothing

How do I actually set up this code so it does what I want it to do correctly?

Answer

Jakob Sternberg picture Jakob Sternberg · Jan 20, 2014

You can use WScript.ScriptFullName which will return the full path of the executing script.


You can then use string manipulation (jscript example) :

scriptdir = WScript.ScriptFullName.substring(0,WScript.ScriptFullName.lastIndexOf(WScript.ScriptName)-1)


Or get help from FileSystemObject, (vbscript example) :

scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)