在VBScript中替换%~dp0

5

我在VBScript中找不到任何替代%~dp0的内容。

On error resume next
Set shell= createobject("WSCRIPT.SHELL")
Shell.run "%~dp0test.bat", vbhide

为什么你需要使用 %~dp0,然后可以使用相对路径如 "test.bat" 调用 .Run 呢? - Panayot Karabakalov
3个回答

5
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheParent = fso.GetParentFolderName(Wscript.ScriptFullName)

来源于此处:

要在脚本中使用所得值,在需要使用其值的变量中添加一个引用即可。例如:

Shell.run GetTheParent & "\test.bat", vbhide

我应该把它放在哪里?我尝试了这个,但它没有起作用。 On error resume next Set fso = CreateObject("Scripting.FileSystemObject") GetTheParent = fso.GetParentFolderName("Wscript.ScriptName") Set shell= createobject("WSCRIPT.SHELL") Shell.run "h4x0r.bat", vbhide - Jordy
我做了一些修复并添加了一个使用示例。 - Andriy M

0

获取执行 .VBS 文件所在的文件夹:

type dp0.vbs
Option Explicit
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
WScript.Echo "WScript.ScriptFullName", WScript.ScriptFullName
WScript.Echo "oFS.GetParentFolderName(WScript.ScriptFullName)", oFS.GetParentFolderName(WScript.ScriptFullName
)


cscript dp0.vbs
WScript.ScriptFullName E:\trials\SoTrials\answers\tools\jscript\ijs\dp0.vbs
oFS.GetParentFolderName(WScript.ScriptFullName) E:\trials\SoTrials\answers\tools\jscript\ijs

我仍然不知道如何将这个应用到我的脚本中。 - Jordy

0

这应该可以工作。

   set objShell = Createobject("wscript.shell")
    strPath = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
    Shell.run strPath & "test.bat", vbhide

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接