Link to home
Start Free TrialLog in
Avatar of kbernstein
kbernstein

asked on

Use a countdown display in a batch .bat file

I have a .bat file that I use for starting a database file.  As you can see below, I put in a ping step to replicate a WAIT, PAUSE or DELAY command, so that the batch waits approximately 25 seconds (accuracy is not critical) before starting the database without the user having to press any buttons.

Is there a way that I can loop through the 25 seconds and show the countdown in the DOS window.  So that they user can see that the pause is working itself down.  Again this would have to be done so that the user does not have to press any buttons.

@Echo Off
Echo Launch Adam - Henry Starter File
Echo The launch will will be delayed 25 seconds
Echo Press ctrl-C to cancel
ping 1.1.1.1 -n 1 -w 25000 >Nul
call "C:\Documents and Settings\syncengine\Desktop\databaseFile.puy"
exit
Avatar of TBK-Consulting
TBK-Consulting
Flag of United States of America image

You can use CLS to clear the screen and then type a number and instead of using the ping you can use a null if then loop and add one to a variable each time and have it type on the screen, then CLS the next loop thru and type the next number ...
Avatar of oBdA
oBdA

For proper delays, you can use sleep.exe from the W2k3 ResKit Tools; install the download on XP or W2k3, you can then copy sleep.exe to wherever you need it (if you install it on the machine you need this script on, it'll be in the path by default).
(Instead of "sleep.exe 1" in the script below, ylou can of course continue to use your ping with a delay of 1000.)
I changed the "call" (which you only need when you start another batch!) with the "start" from the former question.

Windows Server 2003 Resource Kit Tools
http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en

@Echo Off
set /a Timeout = 25
set /a i = 0
Echo Launch Adam - Henry Starter File
Echo The launch will will be delayed 25 seconds
Echo Press Ctrl-C to cancel
Echo Waiting .
:loop
sleep.exe 1
<NUL set /p Dummy=.
set /a i += 1
if %i% LSS %Timeout% goto loop
echo.
start "" "C:\Documents and Settings\syncengine\Desktop\databaseFile.puy"
exit

Open in new window

I have this i my library as a progress indicator ... this does ~20 secs by default - change the line "if %count%==20 goto finish"to amend.

@echo off
mode con: cols=40 lines=4
color 4f
title Please Wait
echo                    0                100
SET /P var=Progress counter : <NUL

set count=0
:loop
  PING -n 2 127.0.0.1 >NUL 2>&1
  call :printline .
  set /a count=count+1
  if %count%==20 goto finish
goto loop

:printline
 REM Print text passed to sub without a carriage return.
 REM Sets line variable in case %1 intereferes with redirect
 set line=%1
 set /p var=%line%<NUL
exit /b

:finish
cls
color 0f
title Finished
mode con: cols=80 lines=25
echo Thankyou, all done now.
pause
exit /b

ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not quite as elaborate as Steve's above, but I like to present different options.  Here's a simpler approach just counting down on the screen.

~bp
@echo off
Echo Launch Adam - Henry Starter File
Echo The launch will will be delayed 25 seconds
Echo Press ctrl-C to cancel
set Delay=25
for /l %%A in (25,-1,1) do (
  echo %%A
  ping -n 2 127.0.0.1 >NUL 2>&1
)
call "C:\Documents and Settings\syncengine\Desktop\databaseFile.puy"

Open in new window

Mine has been much mangled and used but think the idea came from t0t0 or someone else here originally?

Slight variation on my original post:

@echo off
mode con: cols=44 lines=8
color 4f
title Launch Adam - Henry Starter File
echo     Launch Adam - Henry Starter File
echo.
Echo  The launch will will be delayed 25 seconds
Echo            Press Ctrl-C to cancel
echo.
echo               25   20   15   10   5   0 secs
SET /P var=Seconds left: <NUL

set count=0
:loop
  PING -n 2 127.0.0.1 >NUL 2>&1
  call :printline .
  set /a count=count+1
  if %count% GTR 25 goto finish
goto loop

:printline
 REM Print text passed to sub without a carriage return.
 REM Sets line variable in case %1 intereferes with redirect
 set line=%1
 set /p var=%line%<NUL
exit /b

:finish
cls
color 0f
title Finished
mode con: cols=80 lines=25
echo Thankyou, all done now.
call "C:\Documents and Settings\syncengine\Desktop\databaseFile.puy"
exit /b
dragon-it

I've done a lot of behind-the-scene stuff on progress bars etc, but you're right... I brought the "set /p .=.<nul" to EE.

My latest research in this area involves replacing text on the same line.

At the moment I'm busy away from my PC so I will not be participating in EE until after the start of the new year or unless something interesting crops up. I keep dipping in every now and then though...

Avatar of kbernstein

ASKER

Thanks, well done
Actually, the probably first "set /p <NUL" in Experts Exchange is here, by stokesj56: http:Q_21162906.html
Hmmm.... This goes back to one of MY earlier questions regarding the resizing of the DOS CMD window back to it's original size.... This was a major problem for me.

dragon-it, check your mode setting as the CMD window loses it's buffer (and left scrollbar).

Prior to using MODE to alter the size of the DOS box, it is necessary to obtain its settings so that they may be reinstated afterwards.
Here's something somple he could have used:



@echo off

echo.
Echo Launching Adam - Henry Starter File [                          ]<nul

for /l %%a in (0,1,25) do (
   ping -n 1 -w 1000 127.0.0.1 >NUL
   set /p .=.<nul
)
echo.

call "C:\Documents and Settings\syncengine\Desktop\databaseFile.puy"
exit /b
Sorry, I meant:



@echo off

echo.
set /p .=Launching Adam - Henry Starter File [                          ]<nul

for /l %%a in (0,1,25) do (
   ping -n 1 -w 1000 127.0.0.1 >NUL
   set /p .=.<nul
)
echo.

call "C:\Documents and Settings\syncengine\Desktop\databaseFile.puy"
exit /b
Shouldn't that be a -n 2 on the PING otherwise the delay doesn't do anything?

Agreed the mode 80,25 thing is crude way back to full size type window!
And for some reason you can't control-c out of it either?
Anyway I thought you said you weren't really here t0t0?