31

I am putting the finishing touches to a batch script that transfers the contents of a locally edited web site to the internet.

The script opens in a console window, and outputs quite a lot of stuff the administrator needs to see in case something goes wrong. In that case, though, the output is being sent as E-Mail, so there is no need to display the output and confuse the user who runs the update unnecessarily. I need to display only a few lines (say, "starting synchronisation..." and "syncrhonisation complete").

Can anybody think of a way to stop output in a batch script? Kind of a total "echo off"?

A simple

my_batch_file > nul

won't cut it, because as I said, a few things I need to show.

5 Answers 5

41

Windows actually does have the notion of stdout and stderr. Here's an example:

test.bat:

@echo off
echo verbose stuff 1
echo verbose stuff 2
echo verbose stuff 3
echo important stuff! >&2
echo verbose stuff 4

If you run it as 'test.bat' you'll get the full output. If you run it as 'test.bat >nul' then you'll only get the 'important' output (anything redirected to stderr, with the >&2)

Sign up to request clarification or add additional context in comments.

Comments

12

your batch script could run itself

@echo off
if "%1" == "1" goto else
echo start
call %0 1 > nul
echo done
goto done
:else:
echo do stuff
:done:

stdout of the second invocation goes to nul

Comments

2

Sometimes >nul is not enough. In my case I had to do 2>nul. Example:

taskkill /im test.exe 2>nul

Comments

0

You could write the few things that need showing to stderr instead of stdout.

Or alternatively, redirect the outputs of the individual parts that you want hidden.

2 Comments

Sorry, I mixed up my example, I am on a Windows system here. Doesn't work there does it?
Oh you're talking about a literal Batch file. In that case, you could separate the parts that you don't want showing into a separate Batch script, and call it from your primary one, redirecting the output. Also, keep in mind the @ prefix, which stops the command itself from being printed.
0

Combining @ and > NUL worked for me, e.g.

@FC [file1] [file2] > NUL

hides both the command and it's output when executing a batch file.

This script did not stop the display of any error messages.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.