Solved

Windows Batch File - Count Down

Posted on 2016-10-03
4
47 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 23

Accepted Solution

by:
NVIT earned 500 total points
Comment Utility
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
Comment Utility
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 23

Expert Comment

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

Author Comment

by:ReneGe
Comment Utility
Hi NVIT,

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

That's teamwork!

Cheers mate :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Complex Numbers are funny things.  Many people have a basic understanding of them, some a more advanced.  The confusion usually arises when that pesky i (or j for Electrical Engineers) appears and understanding the meaning of a square root of a nega…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now