Running an exe file with parameters in a VBScript

拜拜、爱过 提交于 2019-12-20 04:26:30

问题


I need to create a script that runs setup.exe /configure Install.xml from the folder the script is located.

When I run the script below, it does find the setup.exe but it does not read the parameters. It is like the last part (/configure Install.xml) is not being read.

Script:

Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 
strPath = "setup.exe /configure Install.xml ," & strFolder
objShell.Run strPath

回答1:


Most likely your code doesn't find and run the setup.exe in the script folder, but a different setup.exe somewhere in the %PATH%.

Simply appending the folder to the commandline is not going to do what you want. There are two ways for you to solve this issue:

  • Run setup.exe with the full path, as suggested by @AlexK.. You probably need to provide the full path to Install.xml too. Use the BuildPath method for constructing the paths. You may also want to add quotes around the paths to take care of spaces in them.

    Function qq(str) : qq = """" & str & """" : End Function
    
    strPath = qq(objFSO.BuildPath(strFolder, "setup.exe")) & " /configure " & _
              qq(objFSO.BuildPath(strFolder, "Install.xml"))
    objShell.Run strPath
    
  • Change the working directory to the folder containing your script and setup.exe and run the command without path (or the relative path .\setup.exe).

    objShell.CurrentDirectory = strFolder
    strPath = "setup.exe /configure Install.xml"
    objShell.Run strPath
    



回答2:


Thanks guys! I mixed it up and made this that works for me. (Not sure if something could be made cleaner, but it works!)

Dim WshShell
Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set objShell = CreateObject("Wscript.Shell")
Set WshEnv = WshShell.Environment("PROCESS")
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
objShell.CurrentDirectory = strFolder
WshEnv("SEE_MASK_NOZONECHECKS") = 1 
WshShell.Run("setup.exe /Configure Install.xml"), 0, true 
WshEnv.Remove("SEE_MASK_NOZONECHECKS")
wscript.quit(RetVal)


来源:https://stackoverflow.com/questions/40041460/running-an-exe-file-with-parameters-in-a-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!