Getting current directory in VBScript
Solution 1
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
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
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
'-----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
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
CodeKeyer
Updated on July 09, 2022Comments
-
CodeKeyer almost 2 years
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?
-
CodeKeyer about 11 yearsfor that line
Directory = CurrentDirectory\attribute.exe
i was trying to set a variable. i guess that it doesnt work. as for this codeWinScriptHost.Run curDir & "\testme.bat", 1
i need it to still do its job and run my batch application file without the command window. -
CodeKeyer about 11 yearsdidnt even know you could get down votes but i didnt put it there
-
Jakob Sternberg over 10 yearsNote: This returns the directory from where the script was executed, not necesarily the directory of the script itself.
-
Jakob Sternberg over 10 yearsFor an example, if you drag drop a file onto a batfile which runs the script, then the workingdir will be the directory of that file.
-
Dan about 10 yearsCan you explain what you've done. (Give a man a fish -- feed him for a day, teach a man to fish -- feed him for a lifetime.)
-
Admin about 10 yearsVBScript gets used in several different environments (e.g. Classic ASP, Windows Scripting Host, HTML Application (via IE)). This snippet of code will create an object "App" with a "Path" property. If you stick this in your script you can simply use "App.Path" in your code similarly as you would in VB6. ("Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.")
-
Greg0ry about 9 yearsThe first example need a minor correction: WScript.ScriptFullName.substring(0,WScript.ScriptFullName.lastIndexOf(WScript.ScriptName) - 1)
-
sirdank almost 8 yearsThis property is not set in my scripts.
-
Ansgar Wiechers almost 8 years@sirdank I have no idea what property you're talking about, since there isn't any mentioned in my answer. Care to explain?
-
sirdank almost 8 years@AnsgarWiechers I apologize, I am an idiot. I see now from more careful examination of the question that you are not suggesting
CurrentDirectory
is a global property holding the CWD but that OP is incorrectly constructing his path. I got confused by skimming the question and the answer below. I'll continue trying to retract my downvote but for right now it says my vote is locked. -
Ansgar Wiechers almost 8 yearsOnce a vote is locked you can't retract it until the answer is edited again. Please make sure you fully understand an answer before casting a vote on it from now on.
-
Ekkehard.Horner over 6 yearscurrent directory <> directory of script; dupl of at least two 'use ScriptFullName'-answers; worst practice: Replace instead of .GetParentFolderName().
-
ndemarco over 2 yearsSimplest in VBScript: WScript.Echo Mid(WScript.ScriptFullName, 1, (Len(WScript.ScriptFullName) - Len(WScript.ScriptName)))
-
sekwjlwf over 2 yearsSince OP asked about VBScript, I would like to caution readers on Jakob's first code line, which is an JScript example. It will not work in VBScript, as .substring is a method to an object, but WScript.FullScriptName is just a literal string in VBScript and not an object. @ndemarco's comment right above mine works well.