1

I'm running into an issue while writing a batch file. I wonder if anyone might have a solution for me. This file is very long so I am trying to figure out a way to do less code writing. I have many echo messages that get displayed for a few seconds and then disappear when the file moves on to the next screen/code. I know I can write it something like this...

@echo off
echo Message
ping localhost -n 3 >nul
cls

Writing ping localhost -n 3 >nul every time I want a delay is time consuming. I know I could copy and paste but that is not ideal for my situation either. I had the idea of setting a variable to equal ping localhost -n 3 >nul written as,

set delay3=ping localhost -n 3 >nul

this would allow me to just type %delay3% to save time. I found this functionally works fine but it has a side effect. When written this way,

@echo off
set delay3=ping localhost -n 3 >nul
echo Message
%delay3%
cls

my batch file will display all the ping data on screen even though I've written @echo off at the beginning of my script. Just to be clear, this data only shows up on screen when I use the %delay3% version of the code. Does anyone know of a way to make the ping data not show up on screen when coding it this way?

2 Answers 2

1

How do I make the ping data not show up on screen when coding it this way?

The problem is with your set command:

set delay3=ping localhost -n 3 >nul

The >nul (used to throw away the output) is applied to the set command and is not stored in the variable.

On the other hand:

set "delay3=ping localhost -n 3 >nul"

Does what you want.

Corrected batch file:

@echo off
set "delay3=ping localhost -n 3 >nul"
echo Message
%delay3%
cls
rem do other stuff
endlocal

Example output:

> test
Message

>

Further Reading

0

I confirm DavidPostill's answer is effective.

Another solution is to escape the "greater" sign in the SET command, using the "caret" symbol:

set delay3=ping localhost -n 3 ^>nul

It works the same way for "pipe" sign.

Escaping specials characters can be handy in complex command lines, with combination of FOR, IF, FINDSTR ... and error stream must be redirected to NUL or output stream piped to some other command.

One can also use a more versatile version:

set wait=ping localhost ^>nul -n

and then one can use it with any number of seconds:

%wait% 3
some command
%wait% 20
some other command

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .