Vbscript 用相对路径vbs打开程序
我试图从相对于vbs脚本的路径打开x.txt,脚本位于:“Help file\bin\html\x.txt” 脚本也在帮助文件文件夹中Vbscript 用相对路径vbs打开程序,vbscript,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) otherpa
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()中有一个字符串,但它不允许我在有变量时添加它们。请尝试:
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
你可以用
fileName = fso.BuildPath( _
fso.GetFile( WScript.ScriptFullName ).ParentFolder.Path _
, "\bin\html\x.txt" _
)
x.Run Chr(34) & fileName & Chr(34)
或者更灵活(可以使用脚本文件夹中的相对路径)
在这种情况下,生成的fileName
变量包含空格,因此有必要引用它
fileName = fso.GetAbsolutePathName( fso.BuildPath( _
fso.GetFile( WScript.ScriptFullName ).ParentFolder.Path _
, ".\bin\html\x.txt" _
))
x.Run Chr(34) & fileName & Chr(34)