The problem with the / f command in Windows XP

I am using Windows XP Service Pack 3 (SP3) and the command extensions are included in the Windows Registry by default. One way or another, the following command does not work on this version of Windows, but if I run it on Windows Server 2003 or Windows Vista Business, it works fine. Any clue?

The problem is that in Windows XP it seems that the / f option does not work at all, and part of the command is never executed.

This is the command:

for /f "tokens=1 delims=: " %A in ('tasklist /FI "IMAGENAME eq python.exe" /NH') do ( If "%A" == "python.exe" ( echo "It running" ) Else ( echo "It not running" ) ) 

Thanks in advance.

+3
source share
4 answers

This is because tasklist.exe is displayed on STDERR when no task is found. The for /f loop gets only STDOUT , so if python.exe does not work, it has nothing to do.

Redirecting STDERR to STDOUT ( 2>&1 ) works:

 for /F "tokens=1 delims=: " %A in ('tasklist /FI "IMAGENAME eq python.exe" /NH 2^>^&1') do ( if "%A"=="python.exe" ( echo "It running" ) else ( echo "It not running" ) ) 

The ^ characters are the escape sequences needed to do this.

+7
source

The following works on my computer running Windows XP:

 @echo off for /f "tokens=1 delims=: " %%A in ('tasklist /FI "IMAGENAME eq java.exe" /NH') do ( If "%%A" == "java.exe" ( echo "It running" ) Else ( echo "It not running" ) ) 

Note the use of %%A
(Sorry, I used java.exe because python.exe was not running during my test;))

+1
source

This will work and not display

INFO: no tasks performed with the specified criteria

Message:

 @echo off set found=0 for /f "tokens=1 delims=: " %%A in ('tasklist /NH') do ( If /i "%%A" equ "python.exe" ( set found=1 ) ) if %found%==1 ( @echo It running ) else ( @echo It not running ) 
0
source
 Set RUNNING=False for /f "tokens=1 delims=: " %%a in ('tasklist /FI "IMAGENAME eq python.exe" /NH 2^>NUL') do (Set RUNNING=True) If %RUNNING% == True ( @Echo It IS running ) ELSE ( @Echo It NOT running ) 
0
source

All Articles