Software development, scripting and programming.
User avatar
PROBLEMCHYLD
VIP
Posts: 989
Joined: 2013-03-22 12:55

2015-12-31 07:00 »

How do you add close to a script after so many seconds if an option is not chose.

Example

Option 1 BLah Blah

Option 2 Blaaah Blahhhh

If one of the options is not chose after 5 seconds close the batch.

How to do this anybody?

User avatar
!
30%
Posts: 3259
Joined: 2013-02-25 18:36

2015-12-31 11:52 »

Code: Select all

@ECHO OFF
CHOICE /C YN /N /T 7 /D N /M "[Y]es or [N]o? (Defaults to "No" in 7 seconds.)"
IF %ERRORLEVEL%==1 (
echo YES!
goto :break
)
IF %ERRORLEVEL%==2 (
echo NO!
goto :break
)
:break

User avatar
!
30%
Posts: 3259
Joined: 2013-02-25 18:36

2015-12-31 12:12 »

This is pretty good too, it will show you the actual countdown:

Code: Select all

@ECHO OFF
setlocal enableDelayedExpansion

FOR /l %%N in (7 -1 1) do (
  set /a "min=%%N/60, sec=%%N%%60, n-=1"
  IF !sec! lss 10 set sec=0!sec!
  CLS
  CHOICE /C:YNX /M "Do you want me to say 'Hello world'? [Y/N] (!min!:!sec!) " /T:1 /D:X /N
  IF NOT ERRORLEVEL 3 GOTO :break
)

:break

IF %ERRORLEVEL% == 1 (
echo HELLO WORLD.
)
IF %ERRORLEVEL% == 2 (
echo NO.
)
IF %ERRORLEVEL% == 3 (
echo Nothing was chosen.
)

User avatar
Steven W
VIP
Posts: 2863
Joined: 2013-08-10 22:40

2020-01-24 05:19 »

Self-destructing BAT:

Add this to the end:

(goto) 2>nul & del "%~f0"

Gonna be using this relatively soon :clap:

User avatar
Steven W
VIP
Posts: 2863
Joined: 2013-08-10 22:40

2022-04-10 05:01 »

Was looking into a semi-timer on XP. This sorta sucks, CHOICE, SLEEP and TIMEOUT are not included by default. I'm toying with the idea of using a counter:

pseudo script:

set x=0
:CNTPL:
set /a x+=1
if %x% EQU 9500 GOTO :WHATEVS:
GOTO :CNTPL:

I don't need something really precise, ideally just pause between 3-5 seconds. Counting to 9500 takes approximately 5 seconds on my current test machine. What all would affect that? CPU speed? Cores/Multiple Processorers?

User avatar
Steven W
VIP
Posts: 2863
Joined: 2013-08-10 22:40

2022-04-10 05:07 »

Oh yeah, I don't want this limited to XP, but ideally would run on XP and 2000 without modding. I have seen solutions using PING, but that seems kinda cheesy.

This guy's netsh works well on XP:

https://www.robvanderwoude.com/wait.php

Still kinda a cheesy kludge though.

User avatar
Steven W
VIP
Posts: 2863
Joined: 2013-08-10 22:40

2022-04-10 05:55 »

http://www.nirsoft.net/utils/nircmd.html

Eh, this may be the answer I choose.

Post Reply