我试图运行一个文件但是VBS找不到要运行的文件

时间:2016-10-05 06:26:25

标签: vbscript

我正在以管理员身份通过VBS运行文件文件,不幸的是,这里找不到文件是我正在运行的命令

Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Length = 0 Then
  Set ObjShell = CreateObject("Shell.Application")
  ObjShell.ShellExecute "wscript.exe" _
    , """" & WScript.ScriptFullName & """ RunAsAdministrator", , "runas", 1
  WScript.Quit
End if
CreateObject("WScript.Shell").Run ("C:\Users\David\Desktop\X-tra Settings\File.bat")

它无法找到该文件。我尝试了一些事情。它与VBS文件位于同一文件夹中,但它找不到它。

2 个答案:

答案 0 :(得分:0)

要引用您的变量,我建议您使用此功能:

Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function

你的代码看起来像这样:

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , ""& DblQuote(WScript.ScriptFullName) &" /elevate","","runas", 1
  WScript.Quit
End If
Set Ws = WScript.CreateObject("WScript.Shell")
Ws.Run DblQuote("%userprofile%\Desktop\X-tra Settings\File.bat"),1,True
'****************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************

答案 1 :(得分:0)

您在命令行中缺少空格。这样:

ObjShell.ShellExecute "wscript.exe" _
, """" & WScript.ScriptFullName & """

需要这样(注意wscript.exe之后添加的空格):

ObjShell.ShellExecute "wscript.exe " _
, """" & WScript.ScriptFullName & """

否则它会尝试执行此操作:

wscript.exe"C:\Users\David\Desktop\X-tra Settings\File.vbs"

它无法找到