SLEEP 1000ms

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

SLEEP 1000ms

#1 Post by Ed Dyreen » 17 May 2011 19:39

If I want to pause the script for 1000ms I call a vbscript with only one command in it

Sleep.VBS

Code: Select all

Wscript.Sleep WScript.Arguments(0)*1000

I was wondering if i could do this without writing a vbscript, something like

Code: Select all

CMD.EXE wscript //nologo Wscript.Sleep 1*1000


I don't wan't to use the ping command.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SLEEP 1000ms

#2 Post by dbenham » 17 May 2011 21:17

Try this instead

Code: Select all

timeout /t 1 /nobreak >nul

I'm not sure which version of Windows introduced TIMEOUT, but it is built to do exactly what you are looking for.

Dave Benham

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: SLEEP 1000ms

#3 Post by orange_batch » 18 May 2011 01:00

I wonder if you borrowed that from me 8) nvm probably not

The answer is, sort of. Write the vbscript to a temp file, then it's all contained in your batch.

Code: Select all

set file="%temp%\sleep%random%%random%.vbs"
echo:Wscript.Sleep WScript.Arguments^(0^)*1000>%file%
cscript %file% //nologo 3

:: optional end of script
del %file%


sleep%random%%random% just helps to give the file a unique name. Of course if you want to be sure, you can always program an "if exist" loop and have it keep trying different names.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: SLEEP 1000ms

#4 Post by Ed Dyreen » 18 May 2011 07:16

@dbenham

Code: Select all

timeout /t 1 /nobreak >nul

That's a windows7 command I believe cause XP doesn't recognize it as a valid command.

@orange_batch
You're solution uses a file, that is exactly what I want to avoid :|

I am gonna play a little with it, maybe something like

Code: Select all

start "Sleep" /low /wait "%comspec%" /k "cscript.exe"  "//nologo Wscript.Sleep @time*1000 "

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: SLEEP 1000ms

#5 Post by Ed Dyreen » 18 May 2011 09:15

I am giving up, I think it's not possible. But why?

Code: Select all

cscript <wscript.sleep 1
ERROR: no such file

echo.wscript.sleep 1 |cscript
ERROR: no such file


I wonder where the command actiually gets lost? It is definetly send, but not accepted by cscript. Object related? send, send , send...

The only way this can be done is send ( "wscript.sleep 100" ).

That is more work than using a temp file cscript "sleep.VBS"

How ironic, gotta stop smoking that weed :lol:

Most users live on the other side of the planet i guess..

I never get a response in our timezone.

They are asleep :o

aDutchman

jeb
Expert
Posts: 1058
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: SLEEP 1000ms

#6 Post by jeb » 18 May 2011 12:09

Hi Ed,

Ed Dyreen wrote:Most users live on the other side of the planet i guess..
I never get a response in our timezone.


I'm in your timezone :) , and many others here too ...

And yes, you can use scripts without temporary files, using the hybrid technic.

Code: Select all

@if (@X)==(@Y) @goto :Dummy @end/* Batch part
@echo off
cscript //nologo //e:jscript "%~f0"
goto :eof

Jscript part begins here */
WSH.Sleep(2000);


jeb

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: SLEEP 1000ms

#7 Post by Ed Dyreen » 18 May 2011 12:34

why ''@goto :Dummy @end' there is no Dummy :?:

Code: Select all

@if (@X)==(@Y) @goto :Dummy @end/* Batch part

Don't understand the algorithm :oops:

How can X ever be Y?
and why does it jump to a nonexistend label?
and can I use it with SetLocal EnableExtensions EnableDelayedExpansion?

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: SLEEP 1000ms

#8 Post by aGerman » 18 May 2011 12:56

Haha, exactly... it's a trick. The script code is parsed by cmd.exe and (later) by cscript.exe. That means you have to start the script with a line which is valid for both languages - Batch and JScript. The reason why you need this senseless line is to mask the /* which preludes a comment block in JScript...
Other lines are possible as well, like
@set @junk=0 /*

Regards
aGerman

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: SLEEP 1000ms

#9 Post by Ed Dyreen » 18 May 2011 13:04

:lol:
Last edited by Ed Dyreen on 18 May 2011 13:21, edited 1 time in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: SLEEP 1000ms

#10 Post by Ed Dyreen » 18 May 2011 13:18

It seems that it can only be done once in each batch file and not be nested

Code: Select all

@set @junk=0 /*
:Sleep ()
::(
     ::DOS COMMANDS ONLY
     cscript //nologo //e:jscript "%~f0"
::
goto :eof ()
::)

::JS COMMANDS ONLY; FORCED END OF DOS COMMANDS WOULD BREAK JS
/*

WSH.Sleep(2000);


Last edited by Ed Dyreen on 18 May 2011 13:24, edited 2 times in total.

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: SLEEP 1000ms

#11 Post by aGerman » 18 May 2011 13:22

Makes no sense, but it's possible.

Code: Select all

@if (@X)==(@Y) @goto :Dummy @end/* 1st Batch part
@echo off &setlocal
cscript //nologo //e:jscript "%~f0"
goto FF

Jscript part */
WSH.Sleep(2000);

/* 2nd Batch part
:FF
echo "sleep" done
pause
goto :eof
*/


Regards
aGerman

jeb
Expert
Posts: 1058
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: SLEEP 1000ms

#12 Post by jeb » 18 May 2011 13:22

@aGerman: You are some seconds to fast for me

Ed Dyreen wrote:It seems that it can't be nested

Yes we can :)

Code: Select all

@if (@X)==(@Y) @goto :Dummy @end/* Batch part
@echo off
echo waiting...
call :sleep
echo ready
call :anotherFunc
goto :eof

:sleep
cscript //nologo //e:jscript "%~f0"
goto :eof

Jscript part begins here */
WSH.Sleep(2000);

/*
:anotherFunc
echo %0
goto :eof
rem */


jeb

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: SLEEP 1000ms

#13 Post by Ed Dyreen » 18 May 2011 13:26

And I thought I was good at it :P

Gotta write that shit down

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: SLEEP 1000ms

#14 Post by nitt » 18 May 2011 14:15

Ed Dyreen wrote:I don't wan't to use the ping command.


That's just... I mean, why the heck not?

Code: Select all

@echo off
set /p wait=How long do you want this to wait:
ping 0 -n %wait% > nul
echo Done!


What is wrong with that? It pauses for exactly how long you want it to, and it's better than TIMEOUT because it's universal. I always use this even though I have Windows 7, because for codes I distribute, I cannot use TIMEOUT.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: SLEEP 1000ms

#15 Post by Ed Dyreen » 18 May 2011 14:59

I have had errors on some pc's that did not have a NIC Intel I80386sx 4MB 33Mhz 1982
XP Service pack 0 Service pack 1 was a bit of a problem..
NTLDR remember ? something. It is too long ago

Anyways the purpose is to run nomatter, it is an Unattended script for XP I have a lot of pc-clients.

Post Reply