need a file to echo true if along.bat is running

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gbattis
Posts: 12
Joined: 12 Jan 2015 11:35
Contact:

need a file to echo true if along.bat is running

#1 Post by gbattis » 10 Mar 2015 14:00

I am running a file called along.bat and I need another batch file to echo true or false if the along.bat is running.
I know to get the task list i need to use tasklist /v but how do i make it echo true/false along.bat is running or not.?

SilverHawk
Posts: 17
Joined: 04 Mar 2015 01:42

Re: need a file to echo true if along.bat is running

#2 Post by SilverHawk » 11 Mar 2015 04:08

Usually with the %ERRORLEVEL%.
In the example above I'll look for explorer.exe, in your case I think you've to look for cmd.exe.

Code: Select all

tasklist /FI "IMAGENAME eq explorer.exe" 2> NUL | find /I "explorer.exe"
IF "%ERRORLEVEL%" EQU "0" (
  ECHO Alive!
) ELSE (
  ECHO Dead
)


Watchout that in some environments (I don't remeber if Windows 7 or XP) the find doesn't return the correct ERRORLEVEL: it's always 0 or 1, regardless of the result.
In that case I solve with the use of FOR cycle.

In example, if you want to wait for its termination, you can use:

Code: Select all

:WaitForTermination
FOR /F %%a IN ('tasklist /FI "IMAGENAME eq explorer.exe" 2^> NUL ^| find /I "explorer.exe"') DO (
  ping 127.0.0.1 -n 2 -w 1000
  GOTO WaitForTermination
)

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: need a file to echo true if along.bat is running

#3 Post by Compo » 11 Mar 2015 06:00

The simplest way to achieve this is to have a known Window Title for Tasklist to filter.

If the along.bat doesn't produce a defined console window title add the following before it's first line

Code: Select all

@Title %~nx0

You can then filter with:

Code: Select all

TaskList /fi "WindowTitle eq along.bat"


Perhaps like This:

Code: Select all

@Echo Off
SetLocal
Set "_TFile=along.bat"
For /F "Tokens=*" %%A In (
   'TaskList/V /FI "WindowTitle EQ %_TFile%" /FO CSV /NH') Do (
   For %%B In (%%A) Do Set _Reslt=%%~B)
If /I "%_Reslt%" Equ "%_TFile%" (Echo; [Running]) Else (Echo; [Not Running])
Pause

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: need a file to echo true if along.bat is running

#4 Post by Yury » 11 Mar 2015 07:49

Compo wrote:If the along.bat doesn't produce a defined console window title add the following before it's first line

Code: Select all

@Title %~nx0




Compo, I agree. But the more reliable code will be as follows (given the possible launch from the command line and any special characters in the title):


the first line in "along.bat":

Code: Select all

@tasklist /fi "imagename eq cmd.exe" /fi "windowtitle eq %~f0"|>nul findstr ^^cmd\.exe|| (start "%~f0" cmd /c "%~f0"& exit/b)
;

the content of "another.bat":

Code: Select all

@echo off
set "fullname=C:\Test\along.bat"
tasklist /fi "imagename eq cmd.exe" /fi "windowtitle eq %fullname%"|>nul findstr ^^cmd\.exe&& echo true|| echo false
pause>nul
.

gbattis
Posts: 12
Joined: 12 Jan 2015 11:35
Contact:

Re: need a file to echo true if along.bat is running

#5 Post by gbattis » 11 Mar 2015 10:56

Code: Select all

@Echo Off
SetLocal
Set "_TFile=along.bat"
For /F "Tokens=*" %%A In (
   'TaskList/V /FI "WindowTitle EQ %_TFile%" /FO CSV /NH') Do (
   For %%B In (%%A) Do Set _Reslt=%%~B)
If /I "%_Reslt%" Equ "%_TFile%" (Echo; [Running]) Else (Echo; [Not Running])
Pause

code works great @compo

Post Reply