Run embedded batch files

Author - Kiran Preet

In one of the projects we had a batch file which runs another batch file within it in a loop. This was not working. It used to run once and then used to get stuck.

This is sample code of the outer batch file:

outer.bat:

cd/
cd C:wampbinphpphp5.5.12
php.exe -f "C:wampwwwmycodefile.php"

mycodefile.php contains this code:

<?php
Define('_Dir_', 'C:wampwwwmyfolder');
foreach (new DirectoryIterator(_Dir_) as $file)
{
//run another batch file
system('start /b C:wampwwwinner.bat');
}
?>

inner.bat is the inner batch file which is run in a loop in above php code.

outer.bat used to run only once and then get stuck.

The reason we found was that inner.bat was failing to exit when it was called for the first time in the loop. There was no exit command inside inner.bat which caused the deadlock. So we put exit command at the end of inner.bat contents and it started to work fine.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *