Running a batch command without results on screen
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?
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
- An A-Z Index of the Windows CMD command line
- A categorized list of Windows CMD commands
- redirection - Redirection operators.
- set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.