Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running timeout command in batch file without the timout message appearing

Tags:

batch-file

cmd

I am trying to make a Batch file, and part of it calls for a timeout command for 15 seconds to be called. This is that part of the code...

@echo off
timeout 15
echo The script is waiting to run...
pause

And the following is displayed...

Waiting for 15 seconds, press any key to continue...
The script is waiting to run...
pause

But what I am trying to do is make the message below the timeout line display in place of the timeout message. Is there any way that this can be done?


2 Answers

I think that this should work for you:

@echo off
echo The script is waiting to run...
timeout 15 > nul
pause
like image 166
STLDev Avatar answered Jan 22 '26 05:01

STLDev


Change the order of operations. Print your message first, then do the timeout. It's considered bad form to hide the message from timeout, but redirecting to NUL can work when appropriate. Fifteen seconds isn't pushing it too much, but twenty or more, you need to let the user know they have a safe escape route and you need to deal with the fact that they can do so, whether or not you suppress its output.

I would add that Ctrl-C or Ctrl-Brk are always just a pair of keystrokes away if the user gets impatient, and always lead to even worse user experiences than an extra line in the output of your script.

like image 23
jwdonahue Avatar answered Jan 22 '26 04:01

jwdonahue