Thursday, September 20, 2012

Batch File - How to check if a program is already running and restart it

You can use this in your batch file to check if a program is already running.



@echo off
tasklist /nh /fi "imagename eq wmplayer.exe" | find /i "wmplayer.exe" >nul && (
echo Windows Media Player is running
net stop "Windows Media Player"
net start "Windows Media Player"
) || (
echo Windows Media Player is not running
)
pause>nul


This will check all instances for all users.
If the batch file keeps returning "Running" then most likely you have multiple sessions running in the background that you cannot see in your task manager.
From your command prompt, run tasklist manually to see if there are any ghost processes still running.
Once you ensure that none are running anywhere, the batch file will work correctly.


Original example at http://www.instructables.com/answers/Check-if-a-process-is-running-from-a-batch-file/

No comments:

Post a Comment