Getting current directory in VBScript

VbscriptDirectory

Vbscript Problem Overview


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?

Vbscript Solutions


Solution 1 - Vbscript

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)

Solution 2 - Vbscript

You can use CurrentDirectory property.

Dim WshShell, strCurDir
Set WshShell = CreateObject("WScript.Shell")
strCurDir    = WshShell.CurrentDirectory
WshShell.Run strCurDir & "\attribute.exe", 0
Set WshShell = Nothing

Solution 3 - Vbscript

Your problem is not getting the directory (fso.GetAbsolutePathName(".") resolves the current working directory just fine). Even if you wanted the script directory instead of the current working directory, you could easily determine that as Jakob Sternberg described in his answer.

What does not work in your code is building a path from the directory and your executable. This is invalid syntax:

>

Directory = CurrentDirectory\attribute.exe

If you want to build a path from a variable and a file name, the file name must be specified as a string (or a variable containing a string) and either concatenated with the variable directory variable:

Directory = CurrentDirectory & "\attribute.exe"

or (better) you construct the path using the BuildPath method:

Directory = fso.BuildPath(CurrentDirectory, "attribute.exe")

Solution 4 - Vbscript

'-----Implementation of VB6 App object in VBScript-----
Class clsApplication
	Property Get Path()
          Dim sTmp
          If IsObject(Server) Then
               'Classic ASP
               Path = Server.MapPath("../")
          ElseIf IsObject(WScript) Then 
               'Windows Scripting Host
               Path = Left(WScript.ScriptFullName, InStr(WScript.ScriptFullName, WScript.ScriptName) - 2)
          ElseIf IsObject(window) Then
               'Internet Explorer HTML Application (HTA)
               sTmp = Replace( Replace(Unescape(window.location), "file:///", "") ,"/", "\")
               Path = Left(sTmp, InstrRev( sTmp , "\") - 1)
          End If
	End Property
End Class
Dim App : Set App = New clsApplication 'use as App.Path

Solution 5 - Vbscript

Your line

Directory = CurrentDirectory\attribute.exe

does not match any feature I have encountered in a vbscript instruction manual. The following works for me, tho not sure what/where you expect "attribute.exe" to reside.

dim fso
dim curDir
dim WinScriptHost
set fso = CreateObject("Scripting.FileSystemObject")
curDir = fso.GetAbsolutePathName(".")
set fso = nothing
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run curDir & "\testme.bat", 1
set WinScriptHost = nothing

Solution 6 - Vbscript

Use With in the code.

Try this way :

''''Way 1

currentdir=Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))


''''Way 2

With CreateObject("WScript.Shell")
CurrentPath=.CurrentDirectory
End With


''''Way 3

With WSH
CD=Replace(.ScriptFullName,.ScriptName,"")
End With

Solution 7 - Vbscript

simple:

scriptdir = replace(WScript.ScriptFullName,WScript.ScriptName,"")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCodeKeyerView Question on Stackoverflow
Solution 1 - VbscriptJakob SternbergView Answer on Stackoverflow
Solution 2 - VbscriptPanayot KarabakalovView Answer on Stackoverflow
Solution 3 - VbscriptAnsgar WiechersView Answer on Stackoverflow
Solution 4 - VbscriptsevenfoldView Answer on Stackoverflow
Solution 5 - VbscriptLes FergusonView Answer on Stackoverflow
Solution 6 - Vbscriptuser9556248View Answer on Stackoverflow
Solution 7 - Vbscriptuser3063731View Answer on Stackoverflow