?
Solved

Windows Batch File - Count Down

Posted on 2016-10-03
4
Medium Priority
?
179 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 25

Accepted Solution

by:
NVIT earned 2000 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 25

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
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…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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