Bradley McInerney
Bradley McInerney

Reputation: 1451

Open program with relative path vbs

I am trying to open x.txt from a path relative to the vbs script, the script is in: "Help file\bin\html\x.txt" The script is also in the Help file folder

dim x, fso, vbpath
Set x = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
vbpath = fso.GetParentFolderName(WScript.ScriptFullName)
otherpath = "\bin\html\x.txt"
msgbox(vbpath & otherpath)
x.Run(vbpath & otherpath)

It cannot find the path; the path is msgbox is correct, but its still not finding the path. I know it requires ""s is a string was in the x.Run() but it till not allow me to add them when I have the variables.

Upvotes: 2

Views: 7014

Answers (2)

MC ND
MC ND

Reputation: 70923

You can use

fileName = fso.BuildPath( _ 
    fso.GetFile( WScript.ScriptFullName ).ParentFolder.Path _ 
    , "\bin\html\x.txt" _ 
)

x.Run Chr(34) & fileName & Chr(34)

Or a more flexible (you can use relative paths from the script folder)

fileName = fso.GetAbsolutePathName( fso.BuildPath( _ 
    fso.GetFile( WScript.ScriptFullName ).ParentFolder.Path _ 
    , ".\bin\html\x.txt" _ 
))

x.Run Chr(34) & fileName & Chr(34)

As in this case the generated fileName variable contains spaces, it is necessary to quote it.

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Try:

vbpath = fso.GetParentFolderName(WScript.ScriptFullName)
' use better name, no leading "\" for .BuildPath
suffix = "bin\html\x.txt"
' use std method
fspec = fso.BuildPath(vbpath, suffix)
' no param lst () when calling a sub
MsgBox fspec
' add quotes
fspec = """" & fspec & """"
' check again
MsgBox fspec
' use checked value, instead of repeating the expression
x.Run fspec

Upvotes: 5

Related Questions