Batch file - wait or quit if a process running

0

I have 2 small batch files that change the target location of a junction point (mkref /d), in order to switch between 2 iTunes libraries.

It works well, but I would like it to stop if iTunes is already running - as iTunes will overwrite the library in the wrong target folder if I change the junction while it is running.

Is there a way to quit or pause the batch file if iTunes.exe is running?

mtone

Posted 2012-05-27T20:08:44.913

Reputation: 11 230

Answers

2

Try adding the following to the beginning of your batch files:

tasklist /fi "Imagename eq itunes.exe" 2>NUL | find /i /n "itunes.exe">NUL
if "%ERRORLEVEL%"=="0" exit

For more information, see the documentation on the tasklist and find commands.

Indrek

Posted 2012-05-27T20:08:44.913

Reputation: 21 756

1Perfect, thanks! And with a few GOTOs and TIMEOUT, I made it wait and re-attempt every 5 seconds until iTunes.exe is closed. – mtone – 2012-05-27T21:08:25.667