How to hide command output in CMD?

6

I have a file .bat and there is the command:

chcp 65001

That results in CMD output:

Página de código ativa: 65001

But I want this message, displayed after the command is executed, to be hidden to the user. What is the CMD command that does this?

NOTE: I use Windows 8.

    
asked by Beto 24.08.2016 в 01:27
source

1 answer

8

There are 3 things to do:

  • @echo off

    Hides the commands entered by the user, otherwise it also hides the current folder that is navigating via command. Note that @ is used to hide the response of the echo off command.

  • With echo on (default):

    echo Oi!
    
    pause
    

    Display something like (note a space appears before the first line):

      


      C: \ Users \ User \ Desktop> echo Hi!   Hi!

      C: \ Users \ User \ Desktop> Pause
      Press any key to continue. . .

    See that both responses when commands are displayed

  • With echo off without @:

    echo off
    
    echo Oi!
    
    pause
    

    Display something like (note a space appears before the first line):

      


      C: \ Users \ User \ Desktop> echo off
      Hi!   Press any key to continue. . .

    See that both responses when commands are displayed

  • With echo off com @:

    @echo off
    
    echo "Oi"
    
    pause
    

    Displays something like (note that does not appear a space before the first line):

      

    Hi!   Press any key to continue. . .

  • >nul

    The nul is an object that discards any written data, > is the signal to point to where the output data should be written instead of displaying on the screen, note that for each new command you must add one new >nul , for example:

    @echo off
    
    echo Foo >nul
    
    echo Baz
    
    echo Bar
    
    pause
    

    The answer will be:

      

    Baz
      Bar
      Press any key to continue. . .

    If you do this:

    @echo off
    
    echo Foo >nul
    
    echo Baz >nul
    
    echo Bar >nul
    
    pause
    

    The answer will be:

      

    Press any key to continue. . .

    And with >nul in the command pause you will not have any answer:

    @echo off
    
    echo Foo >nul
    
    echo Baz >nul
    
    echo Bar >nul
    
    pause >nul
    
  • 2>&1

    • The 0 is the stdin , is the input, that is what is sent to the commands, for example commands typed by the user, or commands in a .bat or .sh script
    • The 1 is the stdout , ie the output, or output
    • The 2 is the stderr , error output, when a fault occurs


    In case we do not need 0 because echo off already hides input data, then we use 2 to direct the output to 1, so for example, if you do this:

    @echo off
    
    cd DiretorioInexistente >nul
    
    pause >nul
    

    It will appear something like:

      

    The system can not find the path specified.

    But if you do this it will not display anything:

    @echo off
    
    cd DiretorioInexistente >nul 2>&1
    
    pause >nul
    

Completing

If you want to hide everything but the errors (I think it's preferable), use this:

@echo off

meucomando argumento1 argumento2 >nul

If you want to hide everything, use this:

@echo off

meucomando argumento1 argumento2 >nul 2>&1
  

I do not have much knowledge, if I have a failure to comment or anything.

So the final code looks like this:

@echo off

chcp 65001 >nul 2>&1

echo Olá

pause

Note: When using chcp , a message like this may appear:

  

The system can not write to the specified device.

To "fix", you will have to click on the title bar > Properties > Font > Select the font Lucida Console

    
answered by 24.08.2016 в 03:52