0

I want to run a bat file named Mainfile.bat in location of C:\Users\Administrator\Desktop

Mainfile.bat contents

start cmd /k call C:\Users\Administrator\Desktop\1.bat

start cmd /k call C:\Users\Administrator\Desktop\2.bat

start cmd /k call C:\Users\Administrator\Desktop\3.bat

Once Mainfile.bat started , it should run three bat files specified in the bat file.

To do is , i wrote a code in python like,

subprocess.call("C:\\Users\\Administrator\\Desktop\\Mainfile.bat",stdin=None, stdout=None, stderr=None, shell=False)

print "Finished"

Expected Output:

What want i want is,if i run a Mainfile.bat file ,that should executes the commands.In that, Each bat file in Mainfile.bat take some time to finish the process and it will automatically close once its finished. After finished running all those bat file(1.bat,2.bat,3.bat) only-- i need to print "finished" in console.

Actual output:

But,Once it run Mainfile.bat file , it executes the three bat files and it immediately print "finished" in console..How can i achieve the Expected output?

Got Expected Output :

By changing the content in the Mainfile.bat

(
  start 1.bat
  start 2.bat
  start 3.bat
) | pause

echo done!
pavithran G
  • 112
  • 2
  • 13
  • Try with `shell=True`? Maybe if you specify execution through the shell, it will block till the shell closes. – cs95 Nov 01 '17 at 11:34
  • @cᴏʟᴅsᴘᴇᴇᴅ No, i have tried it , its not working – pavithran G Nov 01 '17 at 11:39
  • You do not need to use **CALL** with the **START** command. That will not fix your problem. Just letting you know that code is not necessary. – Squashman Nov 01 '17 at 12:52
  • 1
    Possible duplicate of [How to wait all batch files to finish before exiting?](https://stackoverflow.com/questions/33584587/how-to-wait-all-batch-files-to-finish-before-exiting) – Squashman Nov 01 '17 at 14:03

1 Answers1

0
    my_process = subprocess.Popen(os.path.abspath('C:\\Users\\Administrator\\Desktop\\Mainfile.bat'), shell=True)
    stdout, stderr = my_process.communicate()
    my_process.wait()

You can use communicate() which will wait until process completes execution.

Mahesh Karia
  • 2,045
  • 1
  • 12
  • 23
  • that's also not working, its prints finished immediately. – pavithran G Nov 01 '17 at 11:46
  • @pavithranG can you try wait() – Mahesh Karia Nov 01 '17 at 11:47
  • All the batch files release control back to the main batch file. So you either monitor for the finished execution of the three batch files or code the batch file so that it does not release control back to the main batch file until all three batch files are done. – Squashman Nov 01 '17 at 12:50
  • @Squashman, i tried like you said for "monitor for the finished execution of the three batch files" ..it worked perfectly – pavithran G Nov 01 '17 at 13:47