I want to add time delay in my batch file. The batch file will be running silently at backgorund. Please help me.
Asked
Active
Viewed 1.5e+01k times
20
-
windows XP, windows 7, Vista both 64 and 32 bit – Ullan Feb 10 '12 at 15:10
-
It is hard to help you without more information. What batch language? What have you tried so far? What system? How long of a delay? _All_ of these items are necessary to provide a good answer. – Mei Feb 10 '12 at 15:10
-
See [windows batch: sleep](http://stackoverflow.com/questions/4317020/windows-batch-sleep). – Dmitry Shkuropatsky Feb 10 '12 at 15:11
-
Just I want to use a DOS commands to run a batch file at the backgorund. The OS I am going to use XP,Vista and Win7. Expecting dleay is amay be about 3-10 seconds. I tried the batch with out delay, but its not deleting a folder since that is used by another application. so I have to wait for few seconds for the exit – Ullan Feb 10 '12 at 15:15
-
The ping command popping up the command prompt.. the batch should run at back end.. can we hide the command prompt.. – Ullan Feb 10 '12 at 15:16
4 Answers
38
timeout 5
to delay
timeout 5 >nul
to delay without asking you to press any key to cancel

The Moo
- 493
- 4
- 6
-
-
There's a `/nobreak` option. Also note that [timeout cannot run in background](https://www.ibm.com/support/pages/timeout-command-run-batch-job-exits-immediately-and-returns-error-input-redirection-not-supported-exiting-process-immediately). – djvg Nov 17 '21 at 15:58
5
ping localhost -n (your time) >nul
example
@echo off
title Test
echo hi
ping localhost -n 3 >nul && :: will wait 3 seconds before going next command (it will not display)
echo bye! && :: still wont be any spaces (just below the hi command)
ping localhost -n 2 >nul && :: will wait 2 seconds before going to next command (it will not display)
@exit
5
You want to use timeout.
timeout 10
will sleep 10 seconds

Boken
- 4,825
- 10
- 32
- 42

fiestacasey
- 627
- 1
- 5
- 10
-
-
you should probably look into using actual programming language to do this then. see http://stackoverflow.com/questions/166044/sleeping-in-a-batch-file for python, c++, perl, etc examples – fiestacasey Feb 10 '12 at 15:15
-
4
0
Ok, yup you use the timeout
command to sleep. But to do the whole process silently, it's not possible with cmd/batch. One of the ways is to create a VBScript that will run the Batch File without opening/showing any window.
And here is the script:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "PATH OF BATCH FILE WITH QUOTATION MARKS" & Chr(34), 0
Set WshShell = Nothing
Copy and paste the above code on notepad and save it as Anyname.**vbs ** An example of the *"PATH OF BATCH FILE WITH QUOTATION MARKS" * might be: "C:\ExampleFolder\MyBatchFile.bat"

Student Seng
- 58
- 5