2

I have this "launchprogram.bat" to call "myprogram.exe" (real name ppbS.exe)

Original code launchprogram.bat

call "ppbS.exe"
set ppbPath=ppbS
set pathHold=%path%
set path=%ppbPath%;%path% 
ppbS create "ppbS" ShowPct 1 No Crawl 1 SetCrawlTime 1 300000
ppbS settext 1 "Start myprogram... Be pacient"
second.bat
ppbS shutdown
set path=%pathHold%
set pathHold=
set ppbPath=

Note: "second.bat" is another bat run hide

extra: This version of "launchprogram.bat" is provided by Noodles

"Cmd /c ""ppbS.exe"" & Dir & set ppbPath=ppbS & set pathHold=%path% & set path=%ppbPath%;%path% & ppbS create ""ppbS"" ShowPct 1 No Crawl 1 SetCrawlTime 1 300000 & ppbS settext 1 ""Start myprogram... Be pacient"" & second.bat & ppbS shutdown"

Anyway; i used this "start.vbs" to call "launchprogram.bat"

On Error Resume Next
mensaje = MsgBox("Start myprogram", vbOKCancel, "myprogram")
If mensaje = vbOK Then
    Script = "launchprogram.bat"
    Set objshell = CreateObject("Wscript.Shell")
    strPath = Wscript.ScriptFullName
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.GetFile(strPath)
    strFolder = objFSO.GetParentFolderName(objFile)
    NewPath = objFSO.BuildPath(strFolder, Script)
    Set objshell = CreateObject("wscript.shell")
    objshell.Run NewPath, vbHide
Else
    Set objshell = CreateObject("Wscript.shell")
    rmensaje = objshell.popup("Cancel myprogram", 3, "myprogram", 16)
End If

As you can see, they are too many scripts and too many codes to call a simple program.

Request:

I want to delete "launchprogram.bat" and put its code into "start.vbs", for use only one script to call "myprogram.exe"

Thanks a lot

Community
  • 1
  • 1
BrianC
  • 149
  • 1
  • 2
  • 9
  • You can put multiple commands on one line. So Execute `cmd /c start "" c:\windows\notepad & Dir & Set Fred=Cat & Set F & Pause`. Because you aren't typing you must use `start` to start programs if you don't want to wait for the program to exit. `Call` is used for starting **batch files**. Normally to start a program you only specify the exe. See my answer here on the three ways to start programs http://stackoverflow.com/questions/31820569/trouble-with-renaming-folders-and-sub-folders-using-batch –  Jul 29 '16 at 22:24
  • There is no point in `On Error GoTo 0` as the last line. The error context is destroyed immediately after that line, so it does nothing but make code slower. Your last three lines in the batch file also do nothing. They run and then the environment they change gets destroyed, so it does nothing but make code slower. –  Jul 29 '16 at 22:29
  • Hi noodles. Thanks. You mean replace the contents of test.bat by: call "myprogram.exe" & Dir & set ppbPath=myprogram & set pathHold=%path% & set path=%ppbPath%;%path% & ppbS create "myprogram" ShowPct 1 No Crawl 1 SetCrawlTime 1 300000 & ppbS settext 1 "Start myprogram... Be pacient" & ppbS shutdown (???). In this case how to put into vbs? – BrianC Jul 29 '16 at 23:14
  • Don't use `call` to start a program. Use the program's name (to wait for program to exit) or `start` (start the program and don't wait) depending on the behaviour desired. `"Cmd /c ""myprogram.exe"" & Dir & set ppbPath=myprogram & set pathHold=%path% & set path=%ppbPath%;%path% & ppbS create ""myprogram"" ShowPct 1 No Crawl 1 SetCrawlTime 1 300000 & ppbS settext 1 ""Start myprogram... Be pacient"" & ppbS shutdown"` A set of opening and closing quotes for VBS. Quotes within the string are escaped so `""` means a single `"` within the string. –  Jul 29 '16 at 23:22
  • Also you can't Change then read vars without turning on a special mode. See `cmd /c set fred=cat & Echo %Fred%` and with special mode (see `cmd /?` and `setlocal /?`) `cmd /v:on /c set fred=cat & echo !fred!`. –  Jul 29 '16 at 23:30
  • Thanks noodles for your great help. Works fine. But my question is how to put this code into my vbs??? – BrianC Jul 29 '16 at 23:36
  • By using `objshell.run` that you are already using. Run above line instead of your batchfile. –  Jul 29 '16 at 23:47
  • You could be more specific and publish the complete response file "vbs" final, to select your answer as correct? Thanks – BrianC Jul 29 '16 at 23:50

2 Answers2

1
objshell.run "Cmd /c ""myprogram.exe"" & Dir & set ppbPath=myprogram & set pathHold=%path% & set path=%ppbPath%;%path% & ppbS create ""myprogram"" ShowPct 1 No Crawl 1 SetCrawlTime 1 300000 & ppbS settext 1 ""Start myprogram... Be pacient"" & ppbS shutdown", vbhide

VBHide isn't defined. If it was it would be 0. As undefined it is treated as 0. This is because constants aren't available in VBScript. It's just luck that VBHide happens to be 0.

0

This is an example to run a python file from vbscript with console and without using a cmd or batch file. The function create the command line string and return the string to run in vbscript. With the third parameter you can set the console status after run: value <= 0 -> console stay open | value > 0 -> console will be closed after a timeout of the value.

Option Explicit

Dim shl, cmdscr
Set shl = CreateObject("WScript.Shell")
cmdscr = cmd_script("Your console title", "path to\YourPythonFile.py", 5)
shl.Run cmdscr, 1, True

Set shl = Nothing
WScript.Quit

Function cmd_script(title, pyfnm, closeAfter)
    ' If closeAfter = 0 then the console stay open else the console close after a timeout of the parameter value
    Dim tmp

    tmp = "%Comspec% /c "
    tmp = tmp & "@echo off & cls & Title " & title
    tmp = tmp & " & python " & pyfnm
    If closeAfter > 0 Then
        tmp = tmp & " & timeout /t " & Cstr(closeAfter) & " /nobreak > NUL"
    Else
        tmp = tmp & " & cmd /k"
    End If
    tmp = tmp & " & exit /B"
    
    cmd_script = tmp
    'msgbox tmp
End function