Computer Hope

Microsoft => Microsoft DOS => Topic started by: stathis on April 27, 2015, 10:49:34 AM

Title: batch file multiple instances
Post by: stathis on April 27, 2015, 10:49:34 AM
Hey everyone. I need help with something.
I have a batch file batch.bat that runs after a torrent finishes performing some standardized routines.

Sometimes it happens, while batch file is still running, that another torrent finishes and batch.bat is triggered again, messing everything up. What is want it to check if batch.bat is still running, then wait to finish and continue with the new instance of batch.bat

Can we do something like this?

Thanks for the help!!
Title: Re: batch file multiple instances
Post by: DaveLembke on April 27, 2015, 11:04:50 AM
If running multiple instances of same batch, its best to have say if your running 4 downloads at the same time a file called batch1, batch2, batch3, and batch4. So that the naming of one of the same code batches doesnt conflict with the other. I use this name_numbering all the time when running automation that looks for instances open or closed etc as well as taskkill routines where you want to specifically kill a process, but the PID is random and you need to kill it by name.

batch1.bat
batch2.bat
batch3.bat
batch4.bat

should fix this
Title: Re: batch file multiple instances
Post by: stathis on April 27, 2015, 11:18:56 AM
I thought of that. Problem though is that utorrent in this case has the "Run Program" option to Run this program when a torrent finishes allowing only 1 file, so the naming scheme you suggest cannot work here
Title: Re: batch file multiple instances
Post by: DaveLembke on April 27, 2015, 11:25:41 AM
Ok... you could install other copies of uTorrent to custom install locations and run multiples of it with single download each instance, and each instance has its own unique batch file name.
Title: Re: batch file multiple instances
Post by: Geek-9pm on April 27, 2015, 11:52:09 AM
Reference:
Running multiple instances of a Batch file in windows simultaneously. (http://stackoverflow.com/questions/23223432/running-multiple-instances-of-a-batch-file-in-windows-simultaneously)
Title: Re: batch file multiple instances
Post by: Lemonilla on April 27, 2015, 11:55:54 AM
Code: [Select]
start /b cmd /c %~0 2>nul 1>&2Assuming I haven't forgotten a crucial part, this will open a new instance of the saved batch file in the background of the current file.  They will share the same output console, and It's very complicated to stop them, but thats how you thread.  I would recommend going with the above answer and doing something like
Code: [Select]
for %%A in (1,1,4) do start batch.bat
Which should open 4 separate instances of batch.bat.

Do a search for snake.bat on this site, (it's a link to a different forum) as an example of how it works.