0

How to make the batch file wait for the first line of command (which executes another batch file) before going to the second line?

When the batch file executes the first line of command, the execution takes time. But the batch file goes to the second line of command without waiting for the first line to finish.

@echo off

"MyOtherBatFile1.bat" "MyArgs1"

echo wait until above batch file is fully executed.

"MyOtherBatFile2.bat" "MyArgs2"

How to fix this?

Codename K
  • 840
  • 3
  • 22
  • 51
  • 2
    Please read my answer on [How to call a batch file that is one level up from the current directory?](https://stackoverflow.com/a/24725044/3074564) It explains all four existing methods to run a batch file from within a batch file. There can be executed in a command prompt window the command `help` to get displayed an incomplete list of [Windows commands](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) which are explained best in my opinion by [SS64.com - A-Z index of Windows CMD commands](https://ss64.com/nt/). – Mofi May 31 '22 at 08:56
  • 2
    If the batch file `MyOtherBatFile1.bat` is in same directory as the main batch file, it would be better to use `call "%~dp0MyOtherBatFile1.bat" "MyArgs1"` as in this case the main batch file works also on current directory being different to the directory containing all the batch files. `%~dp0` is drive and path of argument 0 which is the full path of the batch file currently processed by `cmd.exe`. `%~dp0` is expanded always to a path ending with a backslash. Therefore the concatenation with the batch file name is without an additional backslash for 100% correct file name with absolute path. – Mofi May 31 '22 at 09:00
  • 2
    Further I recommend to read the Microsoft documentation page about [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file). Every programmer writing code for programs or scripts executed on Windows should have read this page at least once carefully and completely to get the knowledge how to reference files and folders on Windows 100% correct. – Mofi May 31 '22 at 09:03

1 Answers1

2

Use Call to start the MyOtherBatFile*.bat batchfiles. See call /? for the correct syntax.

OJBakker
  • 231
  • 2
  • 3