用相对路径vbs打开程序

时间:2014-07-11 09:00:49

标签: vbscript

我试图从相对于vbs脚本的路径打开x.txt,脚本位于:“Help file \ bin \ html \ x.txt” 该脚本也位于帮助文件夹

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)

找不到路径;路径是msgbox是正确的,但它仍然没有找到路径。我知道它需要“”s是x.Run()中的字符串,但是当我有变量时它不允许我添加它们。

2 个答案:

答案 0 :(得分:4)

尝试:

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

答案 1 :(得分:1)

您可以使用

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

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

或者更灵活(您可以使用脚本文件夹中的相对路径)

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

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

在这种情况下,生成的fileName变量包含空格,必须引用它。