[batch-file] Suppress command line output

I have a simple batch file like this:

echo off

taskkill /im "test.exe" /f > nul

pause

If "test.exe" is not running, I get this message:

ERROR: The process "test.exe" not found.

Why does this error message get displayed, even though I have redirected output to NUL?

How can I suppress that output?

This question is related to batch-file

The answer is


Use this script instead:

@taskkill/f /im test.exe >nul 2>&1
@pause

What the 2>&1 part actually does, is that it redirects the stderr output to stdout. I will explain it better below:

@taskkill/f /im test.exe >nul 2>&1

Kill the task "test.exe". Redirect stderr to stdout. Then, redirect stdout to nul.

@pause

Show the pause message Press any key to continue . . . until someone presses a key.

NOTE: The @ symbol is hiding the prompt for each command. You can save up to 8 bytes this way.

The shortest version of your script could be:
@taskkill/f /im test.exe >nul 2>&1&pause
The & character is used for redirection the first time, and for separating the commands the second time.
An @ character is not needed twice in a line. This code is just 40 bytes, despite the one you've posted being 49 bytes! I actually saved 9 bytes. For a cleaner code look above.


You can do this instead too:

tasklist | find /I "test.exe" > nul && taskkill /f /im test.exe > nul

mysqldump doesn't work with: >nul 2>&1
Instead use: 2> nul
This suppress the stderr message: "Warning: Using a password on the command line interface can be insecure"