I have the below snippet of a batch file and my intention is to identify the number of times that command 'export_document_new.bat' is being executed at the moment. If it is greater than 6 jobs, I would want the current processing to wait. Basically if this number of parallel jobs becomes less than 6, I would spawn a new one. However, this little snippet below seems to be behaving very unexpectedly - I always see the count of PROCESS_COUNT set to 1, despite the number of parallel jobs that are underway.
If I have two jobs of export_document_new.bat underway, then before the first one starts, the value of this PRCOESS_COUNT in the parent process apears to be 0. This is expected. When the parent spawns the first export_document_new.bat, I see that the value of this variable in the parent process is now set to 1. Again this is expected. But even if the parent now spawns another process, the value is still being set to 1. This is not expected. I would have expected it to list 2.
Please share your opinions on what I could be missing.
:waitForExportDocumentExes
:CHECK
set /A PROCESS_COUNT=0
for /F "delims=" %%a in ('tasklist /FI "imagename eq cmd.exe" /FO list /v ^| findstr /C:"export_document_new.bat"') do set /a PROCESS_COUNT=%PROCESS_COUNT%+1
if "%PROCESS_COUNT%" GEQ "6" (
sleep 1
goto :CHECK
)
exit /b