Run embedded batch files

|
| By Webner

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.

Leave a Reply

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