Combining two hta codes -  Countdown Timer.hta and Installer.hta

Decarn
Decarn used Ask the Experts™
on
Hi,

I wanted a timer that will prompt the user to install a application by clicking the install button or automatically after X number of minutes. During the entire process, the user is not able to close or minimize the window. The script will then pause for X minutes before verifying that the application is installed successfully and finally allow the user to close the window.

I manage to find the timer code (Countdown Timer.hta)  in one of the thread here but I can't seem to figure out how to run the program in the same .hta code because if the window don't close, there will be an endless loop. Hence I created another .hta code (Installer.hta) to run the program.

I have attached the code snippet here and as you can see, they are very similar. Would appreciate if someone can help to combine both of them into one.

Thanks.
**************** Start of Countdown Timer.hta ***************************
<html>
  <head>
    <HTA:APPLICATION ID="MYHTA"
    APPLICATIONNAME="AT&T Global Network Client Installer"
    BORDER="thin"
    BORDERSTYLE="normal"
    CAPTION="yes"
    MAXIMIZEBUTTON="no"
    MINIMIZEBUTTON="no"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="no"
    VERSION="1.0"
    />
 
    <title>Celestica - AT&T Global Network Client Installation</title>
    <style>
      BODY {font-family: Trebuchet MS, Helvetica, Arial; font-size: 12pt; color: black }
    </style>
  </head>
  
  <body>
    <h2 style="text-align:center;">AT&T Global Network Client</h2> 
    <hr style="width:75%; color: navy;"/>
	<DIV ALIGN=CENTER>
 
    <p>AT&T Global Network Client will be installed in your computer. The installation will take about 15mins and during this time, your <b>network will be disconnected momentarily.</b></p>
	
    <p>Feel free to go for a walk and get some coffee, while the installation takes place.</br>Click the Install Button to install now or it will automatically install in:</p>
	</DIV>
	
    <div id="DynamicText" style="text-align: center; color:red; font-weight: bold">
      <p>&nbsp;</p>
    </div>
 
    <div id="InstallButton" style="visibility: hidden;">
      <p style="text-align: center;"><input type="button" id="InstallNow" value="Install Now"></p>
    </div>
 
	<script language="VBScript">
	' Count Down Code
	' Declare global variable outside the Subs
	Dim intMinutes
	Dim intSeconds
 
	Sub Window_onLoad
      intWidth = 600
      intHeight = 400
      Me.ResizeTo intWidth, intHeight
    Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
      ' Set the minutes and seconds
	  intMinutes = 1
      intSeconds = 10
      iTimerID = window.setInterval("CloseWindow", 1000)
	End Sub
	
	Sub CloseWindow
      If intSeconds = 0 Then
            ' Install auotmatically after time expires
			If intMinutes = 0 Then
				CreateObject("WScript.Shell").Run("Installer.hta")
                window.Close
            Else
                  intMinutes = intMinutes - 1
            End If
            intSeconds = 59
      Else
            intSeconds = intSeconds - 1
      End If
      DynamicText.innerHTML = intMinutes & "min:" & Right("00" & intSeconds, 2) & "s"
	End Sub
	
	InstallButton.Style.Visibility = "visible"
	
     
	'On Click the install button
	Sub InstallNow_onClick()
		CreateObject("WScript.Shell").Run("Installer.hta")
		Window.Close
	End Sub
	
	
    </script>
	
   </body>
</html>
 
*********************** End of Countdown Timer.hta *****************************
 
 
 
**************** Start of Installer.hta ***************************
<html>
  <head>
    <HTA:APPLICATION ID="MYHTA"
    APPLICATIONNAME="AT&T Global Network Client Installer"
    BORDER="thin"
    BORDERSTYLE="normal"
    CAPTION="yes"
    MAXIMIZEBUTTON="no"
    MINIMIZEBUTTON="no"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="no"
    VERSION="1.0"
    />
 
    <title>Celestica - AT&T Global Network Client Installation</title>
    <style>
      BODY {font-family: Trebuchet MS, Helvetica, Arial; font-size: 12pt; color: black }
    </style>
  </head>
  
  <body>
    <h2 style="text-align:center;">AT&T Global Network Client</h2> 
    <hr style="width:75%; color: navy;"/>
	<DIV ALIGN=CENTER>
    <p><b>AT&T Global Network Client is installing...</b></p>
	</DIV>
    <div id="DynamicText" style="text-align: center; color:red; font-weight: bold">
      <p>&nbsp;</p>
    </div>
 
    <div id="CloseWindow" style="visibility: hidden;">
      <p style="text-align: center;"><input type="button" id="CloseMe" value="Close Window"></p>
    </div>
 
    <script language="VBScript">
	
	'Centre the pop up hta window
	Sub Window_onLoad
      intWidth = 600
      intHeight = 400
      Me.ResizeTo intWidth, intHeight
    Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
	End Sub
	
	' AT&T Installation Script
 
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run"""C01 - AT&TGlobal_Netclient_6.9.EXE"""
 
 
   ' Wait for 15mins before checking the log files | hta does not recognise wscript.sleep | 1s = 1000
	Dim dtmStartTime
	dtmStartTime = Now 
    idTimer = window.setTimeout("PausedCheck", 10000, "VBScript")
 
	' Pause then check AT&T log files
    Sub PausedCheck
    
	DIM fso, fso2
 
	'Set Object
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set fso2 = CreateObject("Scripting.FileSystemObject")
 
	'Create Condition
	If (fso.FileExists("C:\WINDOWS\SMSComplete\C01 - AT&TGLOBAL_NETCLIENT_6.9._01.TXT")) OR (fso.FileExists("C:\WINNT\SMSComplete\C01 - AT&TGLOBAL_NETCLIENT_6.9._01.TXT")) Then
	    
		DynamicText.InnerHTML = "AT&T Global Network Client installation successful."
        CloseWindow.Style.Visibility = "visible"
		' Clear Timer
		window.clearTimeout(idTimer)
	Else
        
		DynamicText.InnerHTML = "AT&T Global Network Client installation failed.</br>Please contact Helpdesk at 63602757."
        CloseWindow.Style.Visibility = "visible"
		' Clear Timer
		window.clearTimeout(idTimer)
	End If
		
	End Sub
	
    Sub CloseMe_onClick()
      Window.Close
    End Sub
	
    </script>
   </body>
</html>
**************** End of Installer.hta ***************************

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, are you able to explain what the endless loop is if the window does not close?  Is it with the timer itself?

After running the second HTA, you should use the following line
window.clearTimeout(idTimer)

to prevent the Timer's code from further running.

To put this into a single HTA, instead of running the second one, you could start a second Timer that does the same thing as the second HTA, then Clear the first timer as shown above to stop that one from looping.  This way, you'd pass control from one timer to another.

If you need further help with that, I'll have a look at it tomorrow.  I'm short of time today.

Regards,

Rob.

Author

Commented:
Hi Rob,

Thanks. I did try using window.clearTimeout(idTimer) to clear the timer but the timer still runs. Will appreciate if you can look at it tomorrow. Usually running 2 scripts is ok but I'm pushing it down via SMS and SMS only reports the completion of the first script and not the second one.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, OK this should work fine, although I'm a bit concerned about the logic you've used to see if the install was successful. You're only waiting 10 seconds, and then checking for a text file.

I've changed that, to use objShell.Exec instead of objShell.Run, and then we can wait until the Exec process finishes before checking for the text file.

Regards,

Rob.
<html>
  <head>
    <HTA:APPLICATION ID="MYHTA"
    APPLICATIONNAME="AT&T Global Network Client Installer"
    BORDER="thin"
    BORDERSTYLE="normal"
    CAPTION="yes"
    MAXIMIZEBUTTON="no"
    MINIMIZEBUTTON="no"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="no"
    VERSION="1.0"
    />
 
    <title>Celestica - AT&T Global Network Client Installation</title>
    <style>
      BODY {font-family: Trebuchet MS, Helvetica, Arial; font-size: 12pt; color: black }
    </style>
  </head>
  
  <body>
    <h2 style="text-align:center;">AT&T Global Network Client</h2> 
    <hr style="width:75%; color: navy;"/>
	<DIV ALIGN=CENTER>
 
    <p>AT&T Global Network Client will be installed in your computer. The installation will take about 15mins and during this time, your <b>network will be disconnected momentarily.</b></p>
	
    <p>Feel free to go for a walk and get some coffee, while the installation takes place.</br>Click the Install Button to install now or it will automatically install in:</p>
	</DIV>
	
    <div id="DynamicText" style="text-align: center; color:red; font-weight: bold">
      <p>&nbsp;</p>
    </div>
    
    <div id="InstallButton" style="visibility: hidden;">
      <p style="text-align: center;"><input type="button" id="InstallNow" value="Install Now"></p>
    </div>
 
	<script language="VBScript">
	' Count Down Code
	' Declare global variable outside the Subs
	Dim intMinutes
	Dim intSeconds
	Dim iTimerID1
	Dim iTimerID2
	Dim objExec
 
	Sub Window_onLoad
		intWidth = 600
		intHeight = 400
		Me.ResizeTo intWidth, intHeight
		Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
		' Set the minutes and seconds
		intMinutes = 0
		intSeconds = 10
		iTimerID1 = window.setInterval("AutoCountdown", 1000, "VBScript")
		InstallButton.Style.Visibility = "visible"
		DynamicText.innerHTML = intMinutes & "min:" & Right("00" & intSeconds, 2) & "s"
	End Sub
	
	Sub AutoCountdown
		If intSeconds = 0 Then
			' Install auotmatically after time expires
			If intMinutes = 0 Then
				InstallNow.Click
			Else
				intMinutes = intMinutes - 1
			intSeconds = 59
			End If
		Else
			intSeconds = intSeconds - 1
		End If
		DynamicText.innerHTML = intMinutes & "min:" & Right("00" & intSeconds, 2) & "s"
	End Sub
 
	'On Click the install button
	Sub InstallNow_onClick()
		If InstallNow.Value = "Install Now" Then
			InstallNow.disabled = True
			Window.clearTimeout(iTimerID1)
			DynamicText.innerHTML = "Now installing..."
			iTimerID2 = window.setInterval("PausedCheck", 10000, "VBScript")
			InstallNow.Value = "Close Window"
			Set objShell = CreateObject("WScript.Shell")
			Set objExec = objShell.Exec("""C01 - AT&TGlobal_Netclient_6.9.EXE""")
			'Set objExec = objShell.Exec("""notepad""")
		Else
			window.close
		End If
	End Sub
	
	Sub PausedCheck
		Dim objFSO
	 
		If objExec.Status <> 0 Then
			'Set Object
			Set objFSO = CreateObject("Scripting.FileSystemObject")
		 
			'Create Condition
			If (objFSO.FileExists("C:\WINDOWS\SMSComplete\C01 - AT&TGLOBAL_NETCLIENT_6.9._01.TXT")) OR (objFSO.FileExists("C:\WINNT\SMSComplete\C01 - AT&TGLOBAL_NETCLIENT_6.9._01.TXT")) Then
			    
				DynamicText.InnerHTML = "AT&T Global Network Client installation successful."
		        InstallNow.disabled = False
				' Clear Timer
				window.clearTimeout(iTimerID2)
			Else
				DynamicText.InnerHTML = "AT&T Global Network Client installation failed.</br>Please contact Helpdesk at 63602757."
		        InstallNow.disabled = False
				' Clear Timer
				window.clearTimeout(iTimerID2)
			End If
		End If
	End Sub
    </script>
	
   </body>
</html>

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Hi Rob,

Thanks. That's a cool script. However, there's a small problem - when the timer reaches zero, the application ran but the word "installing..." did not appear. It only shows timer is 00min:00s. When I click the install button, the word "installing..." appear.

May I know why you use objShell.Exec instead of objShell.Run?

Oh and the waiting for 10s is just to facilitate testing. The actual script will wait for 15mins.
Most Valuable Expert 2012
Top Expert 2014
Commented:
OK, I've fixed the text to say when it starts installing....just a small logic problem there inside the counting loop.

The objShell.Run method would run the program, but then you start the PausedCheck timer, which then checks for the existence of one of two text files.  This timer runs at 10 second intervals, and therefore will check for the text files whether the program has finished or not, because there's no way for the Timer to know when the program has finished.

The Exec method, on the other hand, allows us to monitor the status of the programs execution, through the objExec object that I created.  The Status property tells us what state the execution is in.
See: http://msdn.microsoft.com/en-us/library/ateytk4a(VS.85).aspx

If objExec.Status is equal to zero, then the program is still running.  So we check for when it's NOT zero, to know when it has stopped, whether successful or not. Then your file check comes into play to see if it was succesful or not.

I hope that makes sense.

Regards,

Rob.
<html>
  <head>
    <HTA:APPLICATION ID="MYHTA"
    APPLICATIONNAME="AT&T Global Network Client Installer"
    BORDER="thin"
    BORDERSTYLE="normal"
    CAPTION="yes"
    MAXIMIZEBUTTON="no"
    MINIMIZEBUTTON="no"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="no"
    VERSION="1.0"
    />
 
    <title>Celestica - AT&T Global Network Client Installation</title>
    <style>
      BODY {font-family: Trebuchet MS, Helvetica, Arial; font-size: 12pt; color: black }
    </style>
  </head>
  
  <body>
    <h2 style="text-align:center;">AT&T Global Network Client</h2> 
    <hr style="width:75%; color: navy;"/>
	<DIV ALIGN=CENTER>
 
    <p>AT&T Global Network Client will be installed in your computer. The installation will take about 15mins and during this time, your <b>network will be disconnected momentarily.</b></p>
	
    <p>Feel free to go for a walk and get some coffee, while the installation takes place.</br>Click the Install Button to install now or it will automatically install in:</p>
	</DIV>
	
    <div id="DynamicText" style="text-align: center; color:red; font-weight: bold">
      <p>&nbsp;</p>
    </div>
    
    <div id="InstallButton" style="visibility: hidden;">
      <p style="text-align: center;"><input type="button" id="InstallNow" value="Install Now"></p>
    </div>
 
	<script language="VBScript">
	' Count Down Code
	' Declare global variable outside the Subs
	Dim intMinutes
	Dim intSeconds
	Dim iTimerID1
	Dim iTimerID2
	Dim objExec
 
	Sub Window_onLoad
		intWidth = 600
		intHeight = 400
		Me.ResizeTo intWidth, intHeight
		Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
		' Set the minutes and seconds
		intMinutes = 0
		intSeconds = 10
		iTimerID1 = window.setInterval("AutoCountdown", 1000, "VBScript")
		InstallButton.Style.Visibility = "visible"
		DynamicText.innerHTML = intMinutes & "min:" & Right("00" & intSeconds, 2) & "s"
	End Sub
	
	Sub AutoCountdown
		If intSeconds = 0 Then
			' Install auotmatically after time expires
			If intMinutes = 0 Then
				InstallNow.Click
			Else
				intMinutes = intMinutes - 1
				intSeconds = 59
				DynamicText.innerHTML = intMinutes & "min:" & Right("00" & intSeconds, 2) & "s"
			End If
		Else
			intSeconds = intSeconds - 1
			DynamicText.innerHTML = intMinutes & "min:" & Right("00" & intSeconds, 2) & "s"
		End If
	End Sub
 
	'On Click the install button
	Sub InstallNow_onClick()
		If InstallNow.Value = "Install Now" Then
			InstallNow.disabled = True
			Window.clearTimeout(iTimerID1)
			DynamicText.innerHTML = "Now installing..."
			iTimerID2 = window.setInterval("PausedCheck", 10000, "VBScript")
			InstallNow.Value = "Close Window"
			Set objShell = CreateObject("WScript.Shell")
			objShell.Run """C01 - AT&TGlobal_Netclient_6.9.EXE""", 1, True
			'Set objExec = objShell.Exec("""notepad""")
		Else
			window.close
		End If
	End Sub
	
	Sub PausedCheck
		Dim objFSO
	 
		If objExec.Status <> 0 Then
			'Set Object
			Set objFSO = CreateObject("Scripting.FileSystemObject")
		 
			'Create Condition
			If (objFSO.FileExists("C:\WINDOWS\SMSComplete\C01 - AT&TGLOBAL_NETCLIENT_6.9._01.TXT")) OR (objFSO.FileExists("C:\WINNT\SMSComplete\C01 - AT&TGLOBAL_NETCLIENT_6.9._01.TXT")) Then
			    
				DynamicText.InnerHTML = "AT&T Global Network Client installation successful."
		        InstallNow.disabled = False
				' Clear Timer
				window.clearTimeout(iTimerID2)
			Else
				DynamicText.InnerHTML = "AT&T Global Network Client installation failed.</br>Please contact Helpdesk at 63602757."
		        InstallNow.disabled = False
				' Clear Timer
				window.clearTimeout(iTimerID2)
			End If
		End If
	End Sub
    </script>
	
   </body>
</html>

Open in new window

Author

Commented:
Thanks! You have been a great help.
Most Valuable Expert 2012
Top Expert 2014

Commented:
No problem. Thanks for the grade.

Rob.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial