Putting the output of Tasklist into a variable?

Microsoft Windows
Post Reply
Rekrul
Posts: 52
Joined: 2021-Aug-15, 11:29 pm

Putting the output of Tasklist into a variable?

Post by Rekrul »

I'm trying to use the Tasklist command to find out if a particular window is open. Unfortunately, it only seems to work if the window is open. If it's not, the whole thing fails.

Here is the simplest example;

Code: Select all

@echo off
Set Results=
For /f %%A in ('tasklist /nh /v /fi "WINDOWTITLE eq Untitled - Notepad"') do set Results=%%A
echo %Results%
If that window exists, this will print;

notepad.exe

However, if it can't find that window, it will print;

INFO: No tasks running with the specified criteria.
ECHO is off.


It prints the Tasklist message directly instead of putting it into %%A. %%A stays empty, so %Results% is also empty.

How is it bypassing the For command, and how do I prevent this so that the error message goes into %%A rather than being printed directly?
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: Putting the output of Tasklist into a variable?

Post by Simon Sheppard »

When I run your code it returns
INFO:
The first token from the FOR command, which is what I'd expect, if you arent getting that, then something else is going on.
Rekrul
Posts: 52
Joined: 2021-Aug-15, 11:29 pm

Re: Putting the output of Tasklist into a variable?

Post by Rekrul »

Simon Sheppard wrote: 2023-Feb-04, 6:01 pm When I run your code it returns
INFO:
The first token from the FOR command, which is what I'd expect, if you arent getting that, then something else is going on.
The only difference I can think of is that I'm running it on an old XP system. Still, it seems like it should work.

No matter what I try, if the window doesn't exist, nothing is put into the token, so nothing ends up in the variable. It just prints the error message directly.

I suppose testing the variable to see if it's undefined will work for my purposes, but I'm still curious why it doesn't work as expected. I thought the FOR /F command would always be able to grab a command's output.
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: Putting the output of Tasklist into a variable?

Post by Simon Sheppard »

Try running

Code: Select all

tasklist /nh /v /fi "WINDOWTITLE eq Untitled - Notepad" 1>demo.txt
and see if that puts the message into the file, if not it may be using the error stream, so try it with 2>demo.txt
Rekrul
Posts: 52
Joined: 2021-Aug-15, 11:29 pm

Re: Putting the output of Tasklist into a variable?

Post by Rekrul »

Simon Sheppard wrote: 2023-Feb-04, 10:28 pm Try running

Code: Select all

tasklist /nh /v /fi "WINDOWTITLE eq Untitled - Notepad" 1>demo.txt
and see if that puts the message into the file, if not it may be using the error stream, so try it with 2>demo.txt
You're correct, it's using the error stream. I tried Googling this, but didn't find anything helpful. Is there any way to redirect the error stream so that it will be captured by the FOR command?

I know I could direct both to a file and then read the file, but it would be nice if it could be done without creating a temporary file.

EDIT: I found the solution;

Code: Select all

@echo off
Set Results=
For /f %%A in ('tasklist /nh /fi "WINDOWTITLE eq Untitled - Notepad" 2^>^&1') do set Results=%%A
echo %Results%
I knew you could redirect both streams to a file, but I didn't know that the redirect would work by itself to send both streams to standard output.
Post Reply