Articles

How to check if a program is running?

December 9, 2009 at 05:53:24
Specs: Windows XP

I'm new to batch files, but I need to create one that deletes the old copy and then copies a directory (as backup) and everything inside it every day. But the program creating that directory should not be running (the files in the dir should be in a "finished' state). I think I can manage the deleting and copying stuff, so I won't need help with that (I hope ;) ), but how can I check if the program (eg. myprogram.exe) isn't running?

Thanks for any help,
Bart


See More: How to check if a program is running?

Report •


#1
December 9, 2009 at 11:29:53

Xp home or pro?

Xp pro has the tasklist command:

<untested>

:spinlock
tasklist|>nul find /i "program.exe"
if errorlevel 1 (
    echo program.exe not running
) else (
    echo program.exe still running
    rem The "ghost" ping is only to create a delay
    rem to be lighter on the cpu cycles
    >nul ping -n 5 0.0.0.0
    goto spinlock
)

Xp home requires a little more effort, mainly exporting from systeminfo32....


Batch Variable how to


Report •

#2
December 9, 2009 at 12:08:17

Xp home version:

Warning: This won't work right in vista or above.

:spinlock
start /wait "" "%commonprogramfiles%\microsoft shared\msinfo\msinfo32.exe" /categories +SWEnvRunningTasks /report "%temp%\msinforeport"
>nul find /i "program.exe	" "%temp%\msinforeport"

if errorlevel 1 (
 rem the if statement is the same as above
.........


Batch Variable how to


Report •

#3
December 10, 2009 at 03:38:52

Hi Judago,

I have WinXP Home, so this should do the trick. Unfortunately I don't have much time to play around with it at the moment, but in the weekend I'll see if I can get it to work. I'll let you know how it worked out.

Thanks,
Bart


Report •

Related Solutions

#4
December 12, 2009 at 07:40:28

Judago,

I've been playing around with it and it does what I need it to do. I don't really understand what it is what's behind the start command, but that doesn't matter.
One thing I like to, but haven't been able to find an answer to, is how to change the delay to, let's say, an hour. Ie., if the app. is still in memory, just check an hour later again.
I found something like this:
TYPE NUL | CHOICE.COM /N /CY /TY,60 >NUL
(where I could change the 60 to another number of seconds), but it doesn't work, "choice.com" doesn't seem to be on my system.
Is there another way to get this delay done?

Thanks for your help,
Bart


Report •

#5
December 12, 2009 at 13:24:21

I don't really understand what it is what's behind the start command, but that doesn't matter.

Msinfo32 is a gui program that has a few command line switches, but it will sometimes return to the script before it finishes processing, "start /wait" makes sure the next command only executes when it's done. All it's doing is dumping the task list category to a text file in the temp folder.


One thing I like to, but haven't been able to find an answer to, is how to change the delay to, let's say, an hour. Ie., if the app. is still in memory, just check an hour later again.

Xp doesn't have a command to make a delay and as you pointed out, choice isn't present on xp, there aren't many other options(except outside utilities). Most people use ping to cause a delay I tend to use "0.0.0.0" but "localhost" will work also work. This should be close to an hour but it's hard to be accurate using ping.

>nul ping -n 3600 0.0.0.0


Batch Variable how to


Report •

#6
December 12, 2009 at 15:22:44

That made it pretty clear about what happens after start. Next is it searches in the temp-file for the appropriate entry.
But I got choice.com working. I simply downloaded it from the MS ftp-site and moved it into the Windows dir.
I'm now trying to get an echo statement to display the number of times it has been trying while the app. was still running.
I see you're variable expert, so maybe you can help me out on this one.
On the top of the batch-file, right before the :spinlock-label declaration, I put "set i=0". Right after "else", on a new line of course, I put "i=i+1". Then the echo statement: "echo program.exe still running, tried %i% time(s)". I get an error on the i=i+1 as it's not recognized as an internal or external statement, program or batch file. Can you maybe explain how to create such a counter? I also tried the "setlocal enabledelayedexpansion" and !i! option from your tutorial, but this didn't work.

Thanks for your help so far,
Bart


Report •

#7
December 12, 2009 at 20:43:18

You won't need delayed expansion, it's mainly for code blocks and few other special circumstances. Goto's don't suffer from the same problem.

What your after is "set /a" to increment "i".

set /a i+=1

or

set /a i=i+1

or

set /a i=%i%+1

All will work equally well, but I suggest the first to keep things simple, the last would be susceptible the to expansion problem in a code blocks.


Batch Variable how to


Report •

#8
December 12, 2009 at 22:49:14

It just occurred to me that you said you have choice.com,
all versions of choice.com I have seen can't do any more than
99 seconds. If you get choice.exe the limit is a
whole lot larger (probably able to do days).

If you pm me with an email address I can send you a copy if
you don't already have the .exe version.

Also I was thinking you could probably use choice's
nature to your advantage to check on command in addition to
using the time out.

@echo off
set i=0
:spinlock
set /a i+=1
start /wait "" "%commonprogramfiles%\microsoft shared\msinfo\msinfo32.exe" /categories +SWEnvRunningTasks /report "%temp%\msinforeport"
>nul find /i "program.exe	" "%temp%\msinforeport"
if errorlevel 1 (
    echo program.exe not running
) else (
    echo program.exe still running, tried %i% time^(s^)
    echo Enter c to check now
    echo Enter e to exit
    choice.exe /c:ce /t:c,3600 Enter a choice or leave to time out^(timeout is 1 hour^).
    if errorlevel 2 exit /b
    goto spinlock
)

If you please you can even only allow the check choice
(removing the exit option) and still have it timout
after an hour.


Batch Variable how to


Report •

#9
December 13, 2009 at 15:12:46

I have choice.exe now. I downloaded it from the first link you get when you google for "choice.exe download". I also got the com version from there.
And another problem I encountered is solved as well, the ")" that disappeared. It's the ^that does the trick. I'd been stuggling with slashes, but obviously those didn't work.
Today I've een trying to get a safety-device into place: a directory-size comparison, to be sure the action was successfull. Unfortunately nothing I found did the trick.
I'm posting my complete test-script which is doing its action in the Backup directory with some dummy dirs and files in it. Some things are in my native language (Dutch). "Bureaublad" means Desktop (in the directory paths), and "bestand(en)" means file(s). Freemouse.exe is the little utility I use for testing (you should try it, it's really cool and it works on Win7 as well).
There are 2 attempts (maybe not complete) to get the dir-size into a variable, one commented out and one between "==" I hope you can make sense of it ;)
@echo off
setLocal EnableDelayedExpansion

set origdir="C:\Documents and Settings\Bart\Bureaublad\Backup\f1"
set backupdir="C:\Documents and Settings\Bart\Bureaublad\Backup\f2"
set deldir="C:\Documents and Settings\Bart\Bureaublad\Backup\f1\1"
set i=0
:spinlock
start /wait "" "%commonprogramfiles%\microsoft shared\msinfo\msinfo32.exe" /categories +SWEnvRunningTasks /report "%temp%\msinforeport"
>nul find /i "freemouse.exe " "%temp%\msinforeport"

if errorlevel 1 (
echo freemouse.exe not running
echo files are being copied...
rem xcopy %origdir% %backupdir% /e /i /k /h /y

REM for /f "tokens=*" %%a in (
REM dir /w /s %origdir% ^| findstr "bestand(en)"') do (
REM @set infoorig=%%a
REM )
REM echo Original size: %infoorig%

REM for /f "tokens=*" %%a in (
REM dir /w /s %origdir% ^| findstr "bestand(en)"') do (
REM @set infobackup=%%a
REM )
rem echo Backup size : %infobackup%
REM if %infoorig%==%infobackup% (
REM echo Directory will be deleted...
REM rmdir /s /q %deldir%
REM echo Backup was successful!
echo Enter c to check now
echo Enter e to exit
choice.exe /c:ce /t:c,86400 Enter a choice or leave to time out^(timeout is 24 hours^).

REM ) else (
REM echo Backup was unsuccessful!
REM echo Do you want to try again?
REM echo Enter c to check now
REM echo Enter e to exit
REM choice.exe /c:ce /t:c,3600 Enter a choice or leave to time out^(timeout is 24 hours^).
rem goto spinlock
REM )

rem ===============
set /a sum=0
FOR /D %%D in ('Dir /b %origfolder%') do (
FOR /R %%I IN (%%D\*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
)
echo Size is: !sum! kB
rem ================


) else (
cls
set /a i+=1
echo freemouse.exe still running, tried %i% time^(s^)
echo Enter c to check now
echo Enter e to exit
choice.exe /c:ce /t:c,3600 Enter a choice or leave to time out^(timeout is 1 hour^).
if errorlevel 2 exit /b
goto spinlock
)


I hope you can help me for the last time with this size comparison, then I'll keep silent for a while ;)

Thanks,
Bart



Report •

#10
December 13, 2009 at 21:09:37

Most of it looks pretty straight forward it's just this section
that bothers me a little:


rem ===============
set /a sum=0
FOR /D %%D in ('Dir /b %origfolder%') do (
FOR /R %%I IN (%%D\*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
)
echo Size is: !sum! kB
rem ================

This section is kind of there on it's own after all the
work has been done.


Doing things this way you will have quite a rounding
error in the end. I have a script that can divide very large
numbers by 1024 to two decimal places if your interested
(it's just used like a standalone utility). The contents
of the infoorig variable could be used for the calculation.
I wouldn't use set /a on the number alone because the number
may go over it's limits for calculations (32bit or 64bit
signed number).

Here's my interpretation, omitting the folder size in kb.
It assumes a certain dir summery layout, see the second
line.


@ECHO OFF
rem assumes dir output "xxx bestand(en) xxx [whatever-the-word-for-bytes-is]
setLocal EnableDelayedExpansion

set origdir="C:\Documents and Settings\Bart\Bureaublad\Backup\f1"
set backupdir="C:\Documents and Settings\Bart\Bureaublad\Backup\f2"
set deldir="C:\Documents and Settings\Bart\Bureaublad\Backup\f1\1"
set i=0
:spinlock
start /wait "" "%commonprogramfiles%\microsoft shared\msinfo\msinfo32.exe" /categories +SWEnvRunningTasks /report "%temp%\msinforeport"
>nul find /i "freemouse.exe " "%temp%\msinforeport"

if errorlevel 1 (
    echo freemouse.exe not running
    echo files are being copied...
    xcopy %origdir% %backupdir% /e /i /k /h /y

    for /f "tokens=1-3" %%a in ('dir /-c /s %origdir% ^| find /i "bestand(en)"') do (
        set infoorig=%%c
    )
    echo Original size: %infoorig%

    for /f "tokens=1-3" %%a in ('dir /-c /s %backupdir% ^| find /i "bestand(en)"') do (
        set infobackup=%%c
    )
    echo Backup size : %infobackup%
    if %infoorig%==%infobackup% (
        echo Directory will be deleted...
        rmdir /s /q %deldir%
        echo Backup was successful!
        echo Enter c to check now
        echo Enter e to exit
        choice.exe /c:ce /t:c,86400 Enter a choice or leave to time out^(timeout is 24 hours^).
    ) else (
        echo Backup was unsuccessful!
        echo Do you want to try again?
        echo Enter c to check now
        echo Enter e to exit
        choice.exe /c:ce /t:c,3600 Enter a choice or leave to time out^(timeout is 1 hour^).
    )
    if errorlevel 2 exit /b
    goto spinlock
) else (
    cls
    set /a i+=1
    echo freemouse.exe still running, tried !i! time^(s^)
    echo Enter c to check now
    echo Enter e to exit
    choice.exe /c:ce /t:c,3600 Enter a choice or leave to time out^(timeout is 1 hour^).
    if errorlevel 2 exit /b
    goto spinlock
)


Batch Variable how to


Report •

#11
December 14, 2009 at 14:35:44

I already was hoping you'd find it not too messy. Apparently I didn't succeed in that ;) The part between the == lines was a different attempt then the stuff I commented out. Both from different things I found on various sites and fora on the internet. Bytes are just the same btw ;)
But now with the code you assembled from it I get a syntax error: "if == (", without the quotes of course, so there must be something wrong with that. The script terminates right before or on the first if-statement, as the echo statements whether freemouse.exe is running are not, are not executed.
I've been searching like crazy, but I just can't find out why. The only thing I could think of is that both variables are empty disrupting the comparison so I tried to set both infoorig and infobackup vars to 0 just below the first if-statement, but this makes no difference.
Do you have any idea what could be going wrong here?
I also just found out that Xcopy has 5 errorlevels, so maybe I could do something with that. I'll try to get it working with that tomorrow if you can't figure out what's causing the syntax error and I've got the time, but I don't really know what the errorlevels exactly check to determine if xcopy was successful: http://commandwindows.com/xcopy.htm. There's nothing about the size, which I think is the best way to determine if the action was successful.
Well, I give up for today.

Thanks again for you efforts!
Bart


Report •

#12
December 14, 2009 at 19:37:32

I *think* I know what the problem is, would you believe I
forgot to use delayed expansion vars? That's what I get for
staying up late.....

Also xcopy with /v may also prove helpful... I have no idea
what the five errorlevels are though.

@ECHO OFF
rem assumes dir output "xxx bestand(en) xxx [whatever-the-word-for-bytes-is]
setLocal EnableDelayedExpansion

set origdir="C:\Documents and Settings\Bart\Bureaublad\Backup\f1"
set backupdir="C:\Documents and Settings\Bart\Bureaublad\Backup\f2"
set deldir="C:\Documents and Settings\Bart\Bureaublad\Backup\f1\1"
set i=0
:spinlock
start /wait "" "%commonprogramfiles%\microsoft shared\msinfo\msinfo32.exe" /categories +SWEnvRunningTasks /report "%temp%\msinforeport"
>nul find /i "freemouse.exe" "%temp%\msinforeport"

if errorlevel 1 (
    echo freemouse.exe not running
    echo files are being copied...
    xcopy %origdir% %backupdir% /e /i /k /h /y

    for /f "tokens=1-3" %%a in ('dir /-c /s %origdir% ^| find /i "bestand(en)"') do (
        set infoorig=%%c
    )
    echo Original size: !infoorig!

    for /f "tokens=1-3" %%a in ('dir /-c /s %backupdir% ^| find /i "bestand(en)"') do (
        set infobackup=%%c
    )
    echo Backup size : !infobackup!
    if !infoorig!==!infobackup! (
        echo Directory will be deleted...
        rmdir /s /q %deldir%
        echo Backup was successful!
        echo Enter c to check now
        echo Enter e to exit
        choice.exe /c:ce /t:c,86400 Enter a choice or leave to time out^(timeout is 24 hours^).
    ) else (
        echo Backup was unsuccessful!
        echo Do you want to try again?
        echo Enter c to check now
        echo Enter e to exit
        choice.exe /c:ce /t:c,3600 Enter a choice or leave to time out^(timeout is 1 hour^).
    )
    if errorlevel 2 exit /b
    goto spinlock
) else (
    cls
    set /a i+=1
    echo freemouse.exe still running, tried !i! time^(s^)
    echo Enter c to check now
    echo Enter e to exit
    choice.exe /c:ce /t:c,3600 Enter a choice or leave to time out^(timeout is 1 hour^).
    if errorlevel 2 exit /b
    goto spinlock
)


Batch Variable how to


Report •

#13
December 15, 2009 at 12:01:51

Of course I can believe you forgot about the delayed expansion. If I couldn't I had corrected it myself. So don't worry ;) But also the directory variables need to be expansion vars. Otherwise it still won't work. I changed those too and now it's working just fine.
Now I'll have to start puzzling with the script to make it do what it needs to do in reality (check if there's a backup already, if so delete it, then copy the directory tree to the backup dir). I've got all the pieces now, all I need to do is put them in the right location. I hope I won't encounter new riddles again.

Well Judago, you really helped me out creating this. I couldn't have done it without you. Thanks very much!

Bart


Report •

#14
December 15, 2009 at 18:20:48

No problem, glade I could help.


Batch Variable how to


Report •


Ask Question