0

How to call the second shell in this case after pressing pause?

Shell "cmd /c cd %tmp% && echo hello > tmpfile && pause", 1
Shell "cmd /c cd %tmp% && echo hello > tmpfile && pause", 1

and in this case

Shell "cmd /c cd %tmp% && echo hello > tmpfile", 0
Shell "cmd /c cd %tmp% && echo hello > tmpfile", 0
poszkodowany
  • 73
  • 1
  • 6
  • `Shell` is asynchronous - it just returns immediately. I'm not exactly clear on what you are trying to do - a more detailed explanation of the purpose of the code would be helpful in answering this. – Comintern Sep 01 '18 at 14:33
  • I want to execute the code from `cmd.exe /c` and the same after it. – poszkodowany Sep 01 '18 at 14:40
  • That's apparent from the code above. Are you just looking for a way to make a blocking call to `Shell`? – Comintern Sep 01 '18 at 14:43
  • I want the commands to be executed after finishing the previous ones. `cmd.exe /c mycommands` > `cmd.exe /c mycommands` > `cmd.exe /c mycommands` – poszkodowany Sep 01 '18 at 14:46
  • Possible duplicate of [Wait for Shell to finish, then format cells - synchronously execute a command](https://stackoverflow.com/questions/8902022/wait-for-shell-to-finish-then-format-cells-synchronously-execute-a-command) – Comintern Sep 01 '18 at 14:47
  • Can you give an example? I'm not good at programming in VBA. – poszkodowany Sep 01 '18 at 14:51

1 Answers1

1

The Shell command you're using is asynchronous meaning it can run more than one process at a time, so in the case of your two Shell statements in a row, both will be executed simultaneously.

An alternative way is to instead use the Run command of Windows Script Host (WScript.Shell) since it has more options including one to wait for execution of the program to finish before it continues.

Sub ShellWait(fName As String, Optional showWindow As Boolean = True)
    Dim wsh As Object
    Set wsh = VBA.CreateObject("WScript.Shell")
    wsh.Run fName, -showWindow, True
End Sub

You can also hide the window completely by specifying False as the second parameter. (Caution with that option if user input is required!)


For example:

Sub demo()
    ShellWait "x:\test.bat"
    Beep
    MsgBox "Finished running!"
End Sub

More Information:

ashleedawg
  • 20,365
  • 9
  • 72
  • 105