How to hide command output in CMD?

Asked

Viewed 22,304 times

8

I have a file .bat and in it is the command:

chcp 65001

Which results in output on CMD:

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: Use Windows 8.

  • Is that all there is in the batch? If so, I think just put a /K at the end of the instruction.

  • No, there are other instructions. When you put the /K says in the cmd incorrect parameter format - /K

  • Yeah, I think that would only work with one instruction only. I don’t have windows to test at the moment, but you’ve tried to give the instruction echo off before this line? Or maybe put >NUL soon after the instruction you wish to silence. Just stating that I really do not remember, I am telling you everything that came to my mind, hehe.

  • 1

    The " echo off" had already placed. But I didn’t know about this " > NUL ". After I put " > NUL " it worked out that unwanted messages no longer appear. Thank you.

2 answers

14

There are 3 things to be done:

  • @echo off

    Hides the commands typed by the user, apart from this it also hides the current folder that is browsing via command. Note that the @ serves to hide command response echo off.

    1. With echo on (pattern):

      echo Oi!
      
      pause
      

      Displays something like (note appearing a space before the first line):


      C: Users Desktop>echo Hi!
      Hi!

      C: Desktop Users>pause
      Press any key to continue. . .

      See that both responses and commands are displayed

    2. With echo off without @:

      echo off
      
      echo Oi!
      
      pause
      

      Displays something like (note appearing a space before the first line):


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

      See that both responses and commands are displayed

    3. With echo off with @:

      @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, the > is the signal to point out where the output data should be recorded instead of displaying on the screen, note that for each new command you should add a 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 so:

    @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 charge pause will have no 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 commands, for example user-typed commands, or commands in a script . bat or . sh
    • The 1 is the stdout, i.e., output, or output
    • The 2 is the stderr, error output, when some fault occurs


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

    @echo off
    
    cd DiretorioInexistente >nul
    
    pause >nul
    

    Something like:

    System cannot find specified path.

    But if I do this, it won’t show anything:

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

Concluding

If you want to hide everything but the mistakes (I think it’s preferable), use it like this:

@echo off

meucomando argumento1 argumento2 >nul

If you want to hide everything, use it like this:

@echo off

meucomando argumento1 argumento2 >nul 2>&1

I don’t have much knowledge, if you have any faults to comment on or anything.

So the final code can look like this:

@echo off

chcp 65001 >nul 2>&1

echo Olá

pause

Note: When using the chcp may eventually appear a message like this:

The system cannot write to the specified device.

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

  • To avoid long discussions in the comments; the conversation was moved to the chat and can proceed there if anyone is interested, just click on the link.

2


  • Update Redirecionamento de erros + saídas normais para arquivo:


  • Error Output + Standard Output for filing cabinet

  • stderr + stdout for filing cabinet


To hide the possessed mistakes + warnings + exits normal, but redirect these possible outputs to a file, we just point to a filing cabinet receive the redirecting of output 'stderr' to the exit 'stdout': 2>&1 == stderr > & stdout

Basically, doing the redirecting for a filing cabinet, where this will receive the exit stderr who will have his way out, in turn, too redirected to the exit stdout, thus:

commando >.\arquivo.txt strr>&stout

To test this behavior we can run a command that returns a normal output and another error return, already redirecting the error from the error output to and normal output, where we are redirecting to a.txt file.


(chcp 1252 & echo/Saída normal & copy /b 0 nul) >arquivo.txt 2>&1

The commands added in the txt file. as Normal outputs + redirected errors to the standard output:


Página de código ativa: 65001 => of command:chcp 1252

Saída normal=> of command:echo/Saída normal

O sistema não pode encontrar o arquivo especificado. => of command:copy /b 0 nul



In short, the command returns in its default output (stdout) to the filing cabinet: arquivo.txt, where possible output errors stderr are being redirected to the exit stdout.



A short commando that works both in the command line and as in the bat concealing exits, errors and warnings...

>nul chcp 65001 or >nul 2>&1 chcp 65001

Basically redirects output to dispositivo nul no visible output from the control.

  • Some examples of use and your demeanor for

    1. Standard exits = echo/exemplo

    2. Warnings = chcp 65001

    3. Error = dir /o0

inserir a descrição da imagem aqui

@echo off & cls & echo/ 

echo/exemplo 1 :: não vê nada & echo/ 
>nul^
      (
       echo/exemplo 1 :: você não vê nada
       chcp 65001
       dir /o0  ^< este comando gera erro^!! :: Formato de parâmetro incorreto - "0". 
       echo/exemplo 1 :: você continua sem ver nada^!! 
       ) 2>nul

echo/exemplo 2 :: ve soh saida/avisos & echo/
2>nul^
      (
       echo/exemplo 2 :: você não vê possíveis erros mas vê as saídas
       chcp 65001 
       dir /o0   ^< este comando gera erro^!! 
       echo/exemplo 2 :: e você não viu o erro do comando^!!
       echo/
       )

echo/exemplo 3 :: você vê somente possíveis erros & echo/
>nul^
      (
       echo/exemplo 3 ::: você só vê o erro
       chcp 65001
       dir /o0   ^< este comando gera erro^!! :: Formato de parâmetro incorreto - "0".
       echo/exemplo 3 ::: e você soh ver o erro^!! 
       )

rem :: Ou ainda dessa forma ::

@echo off 

rem :: nem erro nem saída ::

dir /o0  >nul 2>&1 ^< esse comando gera erro e você não vê o erro
dir ,    >nul 2>&1 ^< esse comando gera saída e você não vê

  • 2

    If possible leave in the answer also an explanation of these parameters and why this command does what the OP asked.

  • 4

    What does your answer add to the previous one, which is 2 years since writing? I see that yours is just a small topic that was dealt with by the other answer

  • 2

    Dear Kapputz my answer is clean and clear and this is best divided into topics that explain step by step "technically" instead of just giving a code "test there", so with all due respect I disagree with your pinprick about my answer. It is totally necessary to explain each item and how INPUT and OUTPUT works and what types of variations echo off can cause.

  • Rapa, I don’t understand the environment,

  • I put only a short answer, comes operator and asks me p comment, comes another and asks p points differences, comes Voce and expresses "offended/or/pinned"

  • 2

    I’m being polite to you and I’m not offended, the point is that you taped the answer and explained the need for it to be technical, because we’re a site to learn how to program and not to share ready-made things, understand the behavior of a code or API is totally interesting to adapt to different needs, I hope you better understand the community and start to accept constructive criticism without taking it elsewhere. Have a nice day dear kapputz.

  • @Gilherme Nascimento, null is different from null >null creates file, >null hidden output.. has difference, see your answer where picture: echo Foo >null

  • zOK, desaculpa, sorry...

  • @Jeffersonquesado What is your motivation for proposing your comment? Allow me to ask, but your comment was motivated by?

  • @Itwasn'tme going to ask me almost a year after I wrote my comment? Apparently, it was because I saw nothing new in the answer (nor worthy to be written as an answer) when I read it. I will not compare with the current state of the reply, since the writing context was referring to the version of 17/December/2018, not the April/2019

  • @Jeffersonquesado inocente eh inocente em qualquer tempo, c n responder permite entender o entendido! Thanks

  • @Pedrogaspar your posture was relevant and necessary, I understand today, I did not understand at the time, today I am grateful to you, and, I quote, was one of the few relevant and constructive observations operated by user on this platform. I understood that is free the action of responding, however, I understood his comment belatedly. That reply was one of the 5 was first by me posted. Today I understand the need to explain the content posted.

  • Good that I helped somehow @Itwasn'tme, the OS serves for that same, we help each other and create together a great library of content in the area. I did not follow at the time but I saw now that there was a certain discussion because of this post. I also caught a little bit when I arrived and it was a little hard to understand how it works, but that’s the way it is. Many people take it personally, get upset and give up, without trying to understand first how the community works. And there can be no arrogance from those who arrive, have to listen to the elders and try to adapt.

  • It may also seem hard when we receive certain comments, but we have to understand that those who are active, participating in the community for real, end up having to evaluate, edit, comment, respond, a lot during the day, this all reconciling with the real employment of the individual (because everyone here is working for free to build a better community), so you can’t stay being nice and explanatory always, often users choose to be more succinct and objective, and this may sound hard for those who arrive.

  • If you’re interested, I talked a little bit about my initial experience here at Sopt at the goal: https://pt.meta.stackoverflow.com/a/7421/86952, and there’s also the report of other users there, it might be interesting for you and others who come by.

  • @Pedrogaspar much thank you, I will take your advice!

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.