Suppress command line output

前端 未结 4 1233
挽巷
挽巷 2020-11-30 19:05

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:



        
4条回答
  •  孤城傲影
    2020-11-30 19:28

    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.

提交回复
热议问题