Solved

Windows Batch File - Count Down

Posted on 2016-10-03
4
106 Views
Last Modified: 2016-10-04
Hi there,

I have this fun countdown project.

I need to have it displayed in the title in the following format: HH:MM:SS

The following code is as needed.  Only the output has to be modified.

The best solution will be to come up with a math code to change the !CountDown! var back to hh:mm:ss

@ECHO OFF
SETLOCAL EnableDelayedExpansion
CALL :CountDown 01:02:10
ECHO Done
PAUSE
EXIT

:CountDown
FOR /F "tokens=1-3 delims=:" %%A in ("%~1") DO SET /a WaitVal=(%%~A*3600)+(%%~B*60)+(%%~C)
SET Countdown=%WaitVal%
FOR /L %%A in (1,1,%WaitVal%) DO (
	nircmd wait 1000
	SET /a CountDown-=1
	TITLE COUNTDOWN UNTIL [!CountDown!]
)
EXIT /b

Open in new window



Thanks,
Rene
0
Comment
Question by:ReneGe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
NVIT earned 500 total points
ID: 41827437
Make file secstotime.vbs
'Return seconds as hh:mm:ss
intTotalSecs = wscript.arguments(0)
intHours = intTotalSecs \ 3600
intMinutes = (intTotalSecs Mod 3600) \ 60
intSeconds = intTotalSecs Mod 60
WScript.Echo intHours & ":" & intMinutes & ":" & intSeconds

Open in new window


and your .bat file:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
CALL :CountDown 01:02:10
ECHO Done
PAUSE
EXIT

:CountDown
FOR /F "tokens=1-3 delims=:" %%A in ("%~1") DO SET /a WaitVal=(%%~A*3600)+(%%~B*60)+(%%~C)
SET CDN=%WaitVal%
FOR /L %%A in (1,1,%WaitVal%) DO (
	nircmd wait 1000
	SET /a CDN-=1
   for /f "tokens=*" %%a in ('cscript /nologo secstotime.vbs !CDN!') do set CDNValue=%%a
	TITLE COUNTDOWN UNTIL [!CDNValue!]
)
EXIT /b

Open in new window

0
 
LVL 10

Assisted Solution

by:ReneGe
ReneGe earned 0 total points
ID: 41827461
Hi NVIT,

Thank you so much for your help.

Thanks to your script, I figured it out how to do it strictly in my batch file; where %% is the modulus operator.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
CALL :CountDown 11:00:10
ECHO Done
PAUSE
EXIT

:CountDown
FOR /F "tokens=1-3 delims=:" %%A in ("%~1") DO SET /a intTotalSecs=(%%~A*3600)+(%%~B*60)+(%%~C)

SET Countdown=%intTotalSecs%
FOR /L %%A in (1,1,%intTotalSecs%) DO (
	nircmd wait 1000
	SET /a CountDown-=1
		SET /a countdownHH=Countdown/3600
		SET /a "countdownMM=(Countdown%%3600)/60"
		SET /a countdownSS=Countdown %% 60

	IF !countdownHH! LSS 10 SET countdownHH=0!countdownHH!
	IF !countdownMM! LSS 10 SET countdownMM=0!countdownMM!
	IF !countdownSS! LSS 10 SET countdownSS=0!countdownSS!	
	
	TITLE COUNTDOWN UNTIL [!countdownHH!:!countdownMM!:!countdownSS!]
)
EXIT /b

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 41827499
Thanks! I definitely like yours better!
Have a nice day/night...
0
 
LVL 10

Author Comment

by:ReneGe
ID: 41828625
Hi NVIT,

Without your script, I could just not figure it out!

That's teamwork!

Cheers mate :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Have you ever thought of installing a power system that generates solar electricity to power your house? Some may say yes, while others may tell me no. But have you noticed that people around you are now considering installing such systems in their …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question