Page 1 of 1

Batch Countdown

Posted: 31 Mar 2010 21:34
by HKX
Hi everyone,

First post here.

I have come out with a script to stop few windows services:

Code: Select all

ECHO OFF
CLS
:MENU
ECHO.
ECHO ....................................................
ECHO Would you like to stop unnecessary Windows services?
ECHO ....................................................
ECHO.
ECHO 1 - Yes
ECHO 2 - No
ECHO.
SET /P M=Type Y or N, then press ENTER:
IF %M%==Y GOTO STOP01
IF %M%==N GOTO EXIT
:STOP01
net stop "Schedule" /y
net stop "FwcAgent" /y
net stop "SamSs" /y
net stop "ShellHWDetection" /y
net stop "MSSQL$SQLEXPRESS" /y
net stop "Schedule" /y
net stop "LMHosts" /y
net stop "vvdsvc" /y
net stop "ALG" /y
net stop "BITS" /y
net stop "usnjsvc" /y
net stop "SSDPSRV" /y
net stop "TapiSrv" /y
net stop "TermService" /y
net stop "IAANTMON" /y
net stop "EvtEng" /y
net stop "RegSrvc" /y
net stop "S24EventMonitor" /y
net stop "PolicyAgent" /y
net stop "TrkWks" /y
net stop "WmiApSrv" /y
net stop "MBAMService" /y
:EXIT
exit
GOTO MENU



What I need now is a line of text below showing "Services will be automatically stop after X seconds..."
And the X keeps ticking down until it reaches 0. Let's have 5 seconds countdown for this case.
When it reaches 0, I want the program to automatically select Y (Yes) and run the stopping of services.

How do I do this?

Thanks.

Regards

HK

Re: Batch Countdown

Posted: 01 Apr 2010 12:57
by jeb
Hi HKX,

a little bit tricky.

The count of seconds is simple, look at the dos function library. http://www.dostips.com/DtCodeCmdLib.php#sleep

Displaying the Text "Services will be automatically stop after X seconds.." always at the same position can be done
1. Simple with CLS, clear the complete screen before display the text

Code: Select all

:loop
cls
echo Services will be automatically stop after %num% seconds..
call :sleep 1
set /a num=num-1
if %num% GTR 0 goto :loop


2. More Complex: Use CR to return to the Pos1 of a line, print the line without CRLF

Code: Select all

:: create a file a.txt with 2Bytes 0x0d, 0x0d

:: Set CR
for /f "tokens=1" %%f in ('type a.txt') do (
   set CR=%%f
)

echo\
:: Count down from 5 to 0, print each second the line at the same place
for /L %%c in ( 5,-1,0) do (
   set /p ".=Services will be automatically stop after %%c seconds..!CR!" < nul
   call :sleep 1
)


A litte hint. Vista works different as XP with set /p and CR.
In XP you can use set /p ".=!CR!This works on XP" < NUL
In Vista the CR must not to at the first postion, else it will be ignored.

hope it helps
Jan Erik