Solved

Windows Batch File - Count Down

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
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.

867 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

23 Experts available now in Live!

Get 1:1 Help Now