16

I want to add time delay in my batch file. The batch file will be running silently at backgorund. Please help me.

lospejos
  • 1,888
  • 3
  • 17
  • 31
Ullan
  • 1,239
  • 8
  • 19
  • 34
  • 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 Answers4

33

timeout 5

to delay

timeout 5 >nul

to delay without asking you to press any key to cancel

The Moo
  • 443
  • 4
  • 5
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
Community
  • 1
  • 1
Itsproinc
  • 601
  • 5
  • 9
5

You want to use timeout. timeout 10 will sleep 10 seconds

fiestacasey
  • 587
  • 4
  • 9
  • How to use timeout silently.. the cmmand prompt should not be displayed – Ullan Feb 10 '12 at 15:12
  • 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
  • 3
    @HPFE455 - simply redirect the output to nul: `>nul timeout 10` – dbenham Feb 10 '12 at 18:13
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"