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?
Because error messages often go to stderr
not stdout
.
Change the invocation to this:
taskkill /im "test.exe" /f >nul 2>&1
and all will be better.
That works because stdout
is file descriptor 1, and stderr
is file descriptor 2 by convention. (0 is stdin
, incidentally.) The 2>&1
copies output file descriptor 2 from the new value of 1, which was just redirected to the null device.
This syntax is (loosely) borrowed from many Unix shells, but you do have to be careful because there are subtle differences between the shell syntax and CMD.EXE.
Update: I know the OP understands the special nature of the "file" named NUL
I'm writing to here, but a commenter didn't and so let me digress with a little more detail on that aspect.
Going all the way back to the earliest releases of MSDOS, certain file names were preempted by the file system kernel and used to refer to devices. The earliest list of those names included NUL
, PRN
, CON
, AUX
and COM1
through COM4
. NUL
is the null device. It can always be opened for either reading or writing, any amount can be written on it, and reads always succeed but return no data. The others include the parallel printer port, the console, and up to four serial ports. As of MSDOS 5, there were several more reserved names, but the basic convention was very well established.
When Windows was created, it started life as a fairly thin application switching layer on top of the MSDOS kernel, and thus had the same file name restrictions. When Windows NT was created as a true operating system in its own right, names like NUL
and COM1
were too widely assumed to work to permit their elimination. However, the idea that new devices would always get names that would block future user of those names for actual files is obviously unreasonable.
Windows NT and all versions that follow (2K, XP, 7, and now 8) all follow use the much more elaborate NT Namespace from kernel code and to carefully constructed and highly non-portable user space code. In that name space, device drivers are visible through the \Device
folder. To support the required backward compatibility there is a special mechanism using the \DosDevices
folder that implements the list of reserved file names in any file system folder. User code can brows this internal name space using an API layer below the usual Win32 API; a good tool to explore the kernel namespace is WinObj from the SysInternals group at Microsoft.
For a complete description of the rules surrounding legal names of files (and devices) in Windows, this page at MSDN will be both informative and daunting. The rules are a lot more complicated than they ought to be, and it is actually impossible to answer some simple questions such as "how long is the longest legal fully qualified path name?".
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.
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"
You can do this instead too:
tasklist | find /I "test.exe" > nul && taskkill /f /im test.exe > nul
> nul
with >$null
.
Success story sharing
taskkill /im "test.exe" /f >%temp%\nul 2>&1 & del %temp%\nul
. This will prevent a blank null file from being placed onto the local directoryNUL
is a reserved file name, and maps to the NUL device. You cannot create an actual file namedNUL
in any directory.FileStream was asked to open a device that was not a file. For support for devices like 'com1:' or | 'lpt1:', call CreateFile, then use the FileStream constructors that take an OS handle as an IntPtr.