How to check if a process is running via a batch script

后端 未结 18 2290
一个人的身影
一个人的身影 2020-11-22 07:38

How can I check if an application is running from a batch (well cmd) file?

I need to not launch another instance if a program is already running. (I can\'t change th

18条回答
  •  深忆病人
    2020-11-22 08:07

    TrueY's answer seemed the most elegant solution, however, I had to do some messing around because I didn't understand what exactly was going on. Let me clear things up to hopefully save some time for the next person.

    TrueY's modified Answer:

    ::Change the name of notepad.exe to the process .exe that you're trying to track
    ::Process names are CASE SENSITIVE, so notepad.exe works but Notepad.exe does NOT
    ::Do not change IMAGENAME
    ::You can Copy and Paste this into an empty batch file and change the name of
    ::notepad.exe to the process you'd like to track
    ::Also, some large programs take a while to no longer show as not running, so
    ::give this batch a few seconds timer to avoid a false result!!
    
    @echo off
    SETLOCAL EnableExtensions
    
    set EXE=notepad.exe
    
    FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto ProcessFound
    
    goto ProcessNotFound
    
    :ProcessFound
    
    echo %EXE% is running
    goto END
    :ProcessNotFound
    echo %EXE% is not running
    goto END
    :END
    echo Finished!
    

    Anyway, I hope that helps. I know sometimes reading batch/command-line can be kind of confusing sometimes if you're kind of a newbie, like me.

提交回复
热议问题