Apagada docs

Aprendiendo a programar el pasado

Herramientas de usuario

Herramientas del sitio


en:bat:wait_30_seconds

Wait 30 seconds (CMD script batch file)

This is a CMD file for windows that waits for 30 seconds and then continues. You can add it as a section in a batch file where you want the user to wait, or you can copy it as a file in your path and CALL it whenever you need it.

This file: Gets the seconds from %time%. This could be affected by locale. In my es_ES locale, echo “%time%” returns something like “ 8:25:08,37”

So we get positions 5 and 6 from variable time:

set endTime= %time:~-5,2%

After this, we need to remove leading zero, since SET expects numbers with leading zeroes to be octal values. Since 08 and 09 are not valid octal values, we have to remove this zero. I do this by adding a SPACE before the number and then removing the sequence SPACE ZERO.

SET endTime=%endTime: 0= %

Now we can compute the value. If we are waiting 30 seconds, we have to add 30 to seconds, divide by 60 and get the reminder. SET /A understands % as reminder operator, which is problematic, since % is also used to mark variable values, loop values and parameter values. This means:

  1. We should duplicate the % operator (%%) if we are inside a batch file. We should use single % if we are typing in the console (or call batch file like TYPE file | CMD).
  2. Also, we should surround % with spaces, in order to avoid it being interpreted as a variable/loop marker.

The resulting instruction using SET /A to arithmetically assign expressions to the environment variable is:

SET /A endTime=( %endTime% + 30 ) %% 60

If we want to check the time within a single IF condition (without doing all the remove zero magic inside the loop), we should add the leading zero again. After some trial and error, I finally decided to do it via a FOR instruction. It is less elegant but more effective than using set to replace space with zero. The following instruction tests if endTime is between 0 and 9 and replaces it with 00, 01, etc.

for /L %%a in (0;1;9) do if "%endTime%"=="%%a" set endTime=0%%a

Finally, we just need to compare endTime with positions 5 and 6 of environment variable %Time%.

if %time:~-5,2%.==%endTime%. goto :end

Here is the batch file:

wait30seconds.cmd
@echo off
echo Waiting 30 seconds...

rem The following is needed because SET
rem treats numbers starting with 0 as
rem octal values. 
rem (thus 08 and 09 are invalid)

rem 1- Get second and add single space
rem    before.
 
set endTime= %time:~-5,2%

rem 2- Delete zero after space
 
set endTime=%endTime: 0= %

rem 3-Operate
rem
rem %% is duplicated because this is a
rem batch file. Replace with single %
rem if typing in the console.
rem Also we need spaces around numbers
 
set /A endTime=( %endTime% + 30 ) %% 60
echo %endTime%

rem 4-Get zero again
 
for /L %%a in (0;1;9) do if "%endTime%"=="%%a" set endTime=0%%a

rem 5- Finally compare with time seconds
rem until we have a match.

:repeat
if %time:~-5,2%.==%endTime%. goto :end
goto :repeat
:end
Este sitio web utiliza cookies. Al utilizar el sitio web, usted acepta almacenar cookies en su computadora. También reconoce que ha leído y entendido nuestra Política de privacidad. Si no está de acuerdo abandone el sitio web.Más información
en/bat/wait_30_seconds.txt · Última modificación: 2022/11/16 02:51 por nepenthes