Vb script to call an executable

Asked

Viewed 394 times

1

Please someone can help me, I have this code below, saved in vbs, the problem occurs in the two lines that have exenemy, if the code is saved with the first line, it works, if it is saved with the second line it does not find the file, however, the file exists, if I copy and paste in Windows Run it opens the program. The error that appears is: The system cannot find the specified file.

strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell") 
Dim exeName 
Dim statusCode 
exeName = "%windir%\notepad"
exeName = "C:\Program Files (x86)\Lancamentos.net\Lancamentos.exe"
statusCode = WshShell.Run (exeName, 1, true) 
MsgBox("Fim do programa")

2 answers

1


This problem occurs because the path that you are trying to perform has spaces:

C:\Program Files (x86)\Lancamentos.net\Lancamentos.exe

In this case, you should add two extra quotes so that the path be interpreted correctly:

exeName = """Program Files (x86)\Lancamentos.net\Lancamentos.exe"""
  • Thomas, solved friend, thank you very much.

  • Great @userTi. Can you please mark the answer as a solution? Hug

  • Marked, once again, thank you.

0

exeName = "%windir%\notepad"
  • To expand the variable %windir% in the code, you can use:

Set wshShell = CreateObject("WScript.Shell")
WScript.Echo wshShell.ExpandEnvironmentStrings("%windir%")
wshShell = Nothing
        linked with Rob van der Woude’s Sript Pages - Environment Variables

  • You do not need to set a variable to call your program Lancamentos.exe when you will not use a control for possible error/warning, or even pick up the output strings:
strWinDir = (CreateObject("WScript.Shell").ExpandEnvironmentStrings("%windir%"))
CreateObject("WScript.Shell").Run strWinDir&"\notepad.exe",1,false

WScript.CreateObject("WScript.Shell").Run """C:\Program Files (x86)\Lancamentos.net\Lancamentos.exe""",1,true
MsgBox("Fim do programa")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.