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.?
need a file to echo true if along.bat is running
Moderator: DosItHelp
-
- Posts: 17
- Joined: 04 Mar 2015 01:42
Re: need a file to echo true if along.bat is running
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.
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:
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
)
Re: need a file to echo true if along.bat is running
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
You can then filter with:
Perhaps like This:
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
Re: need a file to echo true if along.bat is running
Compo wrote:If the along.bat doesn't produce a defined console window title add the following before it's first lineCode: 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
Re: need a file to echo true if along.bat is running
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