0

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
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47
  • First please fix your code formatting, the label `:waitForExportDocumentExes` is outside of code block, or remove it altogether as its purpose is not clear and also this is wrong `:CHECK set /A PROCESS_COUNT=0 ` put the `set` statement either before or after the label not on the same line. – sst Aug 12 '18 at 22:05
  • 1
    Use `set /a PROCESS_COUNT+=1` instead of `set /a PROCESS_COUNT=%PROCESS_COUNT%+1`. Also, if you want a _numeric comparison_, remove the quotes in `if %PROCESS_COUNT% GEQ 6 (` – Aacini Aug 12 '18 at 22:11
  • You didn't show how you invoke `export_document_new.bat` I assume you've used `start cmd /c export_document_new.bat` since this the only way to start a batch file in parallel. Even then you can't find `export_document_new.bat` in `tasklist`'s output because `tasklist` does not report processes command line. For it to work you have set windows title from `export_document_new.bat` and add another filter to tasklist argument `/FI "windowtitle eq something"` no findstr needed. But I recommend you to refer to this [Answer](https://stackoverflow.com/a/11715437/4561332) for parallel task management – sst Aug 12 '18 at 22:23

0 Answers0