2

I have two batch files. The first one is to download files from SFTP. The second is to manipulate some local files. Since I have to wait until the first one is finished, how to hold the program in VBA so the batch files can be executed one by one?

Right now I am using

Call Shell("Bat1.bat")
Call Shell("Bat2.bat")

However, two batch files will be executed at the same time. Bat2.bat will not wait Bat1.bat.

Thanks

Community
  • 1
  • 1
NewGuyComesIn
  • 189
  • 1
  • 2
  • 14

1 Answers1

1

First off, drop the Call statement, it's not needed.

Shell "Bat1.bat"

Make Bat1.bat create an empty "marker" file when it's done, e.g. bat1.ready. Then make your VBA code loop until it can find that .ready file:

While Dir("bat1.ready") = vbNullString
    DoEvents
Wend

This should keep your UI/host app responsive, while essentially doing nothing until the marker file is up.

Next you delete the marker file and proceed with bat2:

Kill "bat1.ready"
Shell "Bat2.bat"

Ta-da!


The Shell function returns immediately, and returns the created TaskID. If you can't modify the batch file in any way (or ask whoever can modify it if they could do that), then you'll probably need to use some Win32 calls to use that TaskID to determine whether the task has completed, as in the ShellAndWait link that Tim Williams provided in a comment

Community
  • 1
  • 1
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235