Solved Windows Command Prompt sleep using %time% variable

February 28, 2013 at 08:54:23
Specs: Windows 7, 4GB
Hello,

I need to make a peace of code that would make my program to output text when time is going to be 5 seconds more from now..
4 example:

set CurrentTime = %time%
set CurrentTime = %CurrentTime% + 5 seconds
:wait
if %time% EQU %CurrentTime%==goto work
goto wait
:work
cls
echo I have been waiting for 5 seconds && pause :)

This is kind of what I need... :\ Is it possible to do that? I would like to do that without sleep.exe timeout.exe or any vbscript... Because I don't want any unnecessary, annoying process... :)

Thank you 4 your help :P


See More: Windows Command Prompt sleep using %time% variable

Report •

#1
February 28, 2013 at 12:03:27
Ah, "timeout" was gonna be my first suggestion, but if you want to code it:
@echo off & setlocal
set /p secs=number of seconds to wait:
set a=0
set z=a
:loop
call :xx
if %a% equ %z% goto :loop
set /a c+=1
echo a: count: %c% a:%a% z:%z% time: %time%
if %c% equ %secs% goto :zzz
set z=%a%
goto :loop

:xx
set a=%time:~-5%
set /a a+=0
goto :eof

:zzz
echo done


Report •

#2
February 28, 2013 at 12:10:04
Thank you very much. This is exactly what I was looking for... :)
BUT. Lets say I want to use this script more than once in my program how shut I put it into my code? Because when I just paste it on my ping commands, it just stops... :\ It is kind of confusing but it is exactly what I need :) So I repeat: How should I use this code more than once in program.?..(lets say once for 3 seconds, once for 5, and once for 6) :)

Report •

#3
February 28, 2013 at 19:35:26
✔ Best Answer
just make it a sub:
::==== begin batch: use :timer to time 6, then 10 second intervals.
call :timer 6
call :timer 10
goto :eof

:timer
set a=0
set z=%a%
set c=0
:loop
call :xx
if %a% equ %z% goto :loop
set /a c+=1
echo a: count: %c% a:%a% z:%z% time: %time%
if %c% equ %1 goto :eof
set z=%a%
goto :loop
:xx
set a=%time:~-5%
set /a a+=0
goto :eof
::====== end batch
the sub can be an external batch or an internal label. I also fixed a stupid mistake:
set z=a
fixed to:
set z=%a%
(or just "set z=0")
Also, be aware that partial second-value in first loop will cause inaccuracy in actual timing. At beginning of loop, the second-count might be like 20.99, which throws the timing off almost a whole second. This can be fixed if it's an issue:
:timer
call :xx
set d=%b%
set z=%a%
set c=0
:loop
call :xx
if %a% equ %z% goto :loop
if %b% lss %d% goto :loop
set /a c+=1
echo count: %c% a:%a% z:%z% time: %time%
if %c% equ %1 goto :eof
set z=%a%
goto :loop
:xx
set a=%time:~-5%
set b=%a:.=%
set /a b="b%%100"
set /a a+=0
goto :eof


Report •

Related Solutions

#4
March 1, 2013 at 05:04:32
Well thank you... I'll give it a try... :)

Report •

Ask Question