Solved

Windows Batch File - Count Down

Posted on 2016-10-03
4
72 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bash script to unzip a file 5 54
how to find then copy to anotehr file 3 52
One Standard Deviation from the Mean 2 25
Math equations 13 50
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This article provides a brief introduction to tissue engineering, the process by which organs can be grown artificially. It covers the problems with organ transplants, the tissue engineering process, and the current successes and problems of the tec…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
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…

832 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