Link to home
Start Free TrialLog in
Avatar of Rebel_no_1
Rebel_no_1Flag for China

asked on

How do I run a .bat file (onload) from an .hta (HTML application)?

I am building a type of interface for a script and need the .hta interface to load this script when it starts. Therefore I will run the Screen.hta and it must run the script called Startup.bat. Everything is in the same root folder. Also, where do I place the snippet in the below script for it to execute correctly? I have looked everywhere on the net and tried several examples but could not get it to work.

<html>
<head>
<HTA:APPLICATION
     ID="objScreen" 
     APPLICATIONNAME="Screen" 
     BORDER="none" 
     BORDERSTYLE="normal" 
     CAPTION="no" 
     ICON="" 
     MAXIMIZEBUTTON="no" 
     MINIMIZEBUTTON="no" 
     SHOWINTASKBAR="yes" 
     SINGLEINSTANCE="yes" 
     SYSMENU="no" 
     SCROLL="no" 
     VERSION="1.0" 
     CONTEXTMENU="no" 
     WINDOWSTATE="maximize"
     INNERBORDER="no"/> 




</head>



<SPAN
   STYLE="
      top: 203;
      left: 572;
      width: 42px;
      height: 43px; 
      position: absolute;
      z-index: 1;
      background-image: url('Busy.gif');
      visibility: show;">
</SPAN>
<SPAN
   STYLE="
      top: 263;
      left: 572;
      width: 42px;
      height: 43px; 
      position: absolute;
      z-index: 1;
      background-image: url('Busy.gif');
      visibility: show;">
</SPAN>
<SPAN
   STYLE="
      top: 323;
      left: 572;
      width: 42px;
      height: 43px; 
      position: absolute;
      z-index: 1;
      background-image: url('Busy.gif');
      visibility: show;">
</SPAN>
<SPAN
   STYLE="
      top: 383;
      left: 572;
      width: 20px;
      height: 20px; 
      position: absolute;
      z-index: 1;
      background-image: url('Busy.gif');
      visibility: show;">
</SPAN>
<SPAN
   STYLE="
      top: 443;
      left: 572;
      width: 42px;
      height: 43px; 
      position: absolute;
      z-index: 1;
      background-image: url('Busy.gif');
      visibility: show;">
</SPAN>
<SPAN
   STYLE="
      top: 503;
      left: 572;
      width: 42px;
      height: 43px; 
      position: absolute;
      z-index: 1;
      background-image: url('Busy.gif');
      visibility: show;">
</SPAN>
<SPAN
   STYLE="
      top: 563;
      left: 572;
      width: 42px;
      height: 43px; 
      position: absolute;
      z-index: 1;
      background-image: url('Busy.gif');
      visibility: show;">
</SPAN>





<Script Language="VBScript"> 

   Sub Window_OnLoad 
      ' Size and position main window
      window.resizeTo 1366,768 
      window.moveTo 0,0

      ' Display current time
      myClock.innerText = Now()

      ' Set up a timer event so we can update the screen periodically
      iTimerID = window.setInterval("myVBSClock", 1000) 
   End Sub    

   Sub myVBSClock 
      ' Update current time
      myClock.innerText = Now()

      ' Refresh colors from status file
      CheckStatusFile
   End Sub 

   Function ComputerName()
      ' Get computername
      Set wshShell = CreateObject("WScript.Shell")
      ComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
      Set wshShell = Nothing
   End Function

   Sub CheckStatusFile
      ' Constants for file I/O
      Const ForReading = 1
      Const ForWriting = 2
      Const TriStateUseDefault = -2

      ' Name of status file
      strStatusFile = "Status.txt"

      ' Create file system object
      Set objFSO = CreateObject("Scripting.FileSystemObject")

      ' Make sure status file exists
      If objFSO.FileExists(strStatusFile) Then
         ' Open it for reading
         Set objStatusFile = objFSO.OpenTextFile(strStatusFile, ForReading, False, TriStateUseDefault)
         ' Read status file, slit lines into array elements
         arrStatus = Split(objStatusFile.ReadAll, vbCrLf)
         ' Close file, release objects
         objStatusFile.Close
         Set objStatusFile = Nothing
         Set objFSO = Nothing

         ' Process each line of the status file (skip blank lines)
         For Each strStatus in arrStatus
            If strStatus <> "" Then
               ' Split status line into array at comma
               arrField = Split(strStatus, ",")
               ' Process lines based on first field on line (item name to update), and set item color to status file value
               Select Case arrField(0)
                  Case "Security-Environment"
                     SecurityEnvironment.style.background = arrField(1)
                  Case "Help-Menu"
                     HelpMenu.style.background = arrField(1)
                  Case "Biometric-License"
                     BiometricLicense.style.background = arrField(1)
                  Case "Card-Printer"
                     CardPrinter.style.background = arrField(1)
                  Case "GPS-Device"
                     GPSDevice.style.background = arrField(1)
                  Case "Battery-Controller"
                     BatteryController.style.background = arrField(1)
                  Case "Biometric-Application"
                     BiometricApplication.style.background = arrField(1)
               End Select
            End If
         Next 
      End If
   End Sub

</SCRIPT> 


<BODY
     BACKGROUND="bgimageV3.jpg"
     TOPMARGIN="10"
     RIGHTMARGIN="10"
     LEFTMARGIN="10"
     BGCOLOR="#7e7e7e"> 

<SPAN
   id="SecurityEnvironment"
   style="
      top: 200;
      left: 47;
      width: 570px;
      height: 40px; 
      position: absolute;
      z-index: 1;
      background: grey;
      border-color: #000000;
      border-style: solid;
      border-top-width: 1px;
      border-bottom-width: 1px;
      border-left-width: 1px;
      border-right-width: 1px;
      text-align: left;
      color: white;
      font-family: Arial;
      font-size : 40;
      visibility: show;">
SECURITY ENVIRONMENT
</SPAN>

<SPAN
   id="HelpMenu"
   style="
      top: 260;
      left: 47;
      width: 570px;
      height: 40px; 
      position: absolute;
      z-index: 1;
      background: grey;
      border-color: #000000;
      border-style: solid;
      border-top-width: 1px;
      border-bottom-width: 1px;
      border-left-width: 1px;
      border-right-width: 1px;
      text-align: left;
      color: white;
      font-family: Arial;
      font-size : 40;
      visibility: show;">
HELP MENU
</SPAN>

<SPAN
   id="BiometricLicense"
   style="
      top: 320;
      left: 47;
      width: 570px;
      height: 40px; 
      position: absolute;
      z-index: 1;
      background: grey;
      border-color: #000000;
      border-style: solid;
      border-top-width: 1px;
      border-bottom-width: 1px;
      border-left-width: 1px;
      border-right-width: 1px;
      text-align: left;
      color: white;
      font-family: Arial;
      font-size : 40;
      visibility: show;">
BIOMETRIC LICENSE
</SPAN>

<SPAN
   id="CardPrinter"
   style="
      top: 380;
      left: 47;
      width: 570px;
      height: 40px; 
      position: absolute;
      z-index: 1;
      background: grey;
      border-color: #000000;
      border-style: solid;
      border-top-width: 1px;
      border-bottom-width: 1px;
      border-left-width: 1px;
      border-right-width: 1px;
      text-align: left;
      color: white;
      font-family: Arial;
      font-size : 40;
      visibility: show;">
CARD PRINTER
</SPAN>

<SPAN
   id="GpsDevice"
   style="
      top: 440;
      left: 47;
      width: 570px;
      height: 40px; 
      position: absolute;
      z-index: 1;
      background: grey;
      border-color: #000000;
      border-style: solid;
      border-top-width: 1px;
      border-bottom-width: 1px;
      border-left-width: 1px;
      border-right-width: 1px;
      text-align: left;
      color: white;
      font-family: Arial;
      font-size : 40;
      visibility: show;">
GPS DEVICE
</SPAN>

<SPAN
   id="BatteryController"
   style="
      top: 500;
      left: 47;
      width: 570px;
      height: 40px; 
      position: absolute;
      z-index: 1;
      background: grey;
      border-color: #000000;
      border-style: solid;
      border-top-width: 1px;
      border-bottom-width: 1px;
      border-left-width: 1px;
      border-right-width: 1px;
      text-align: left;
      color: white;
      font-family: Arial;
      font-size : 40;
      visibility: show;">
BATTERY CONTROLLER
</SPAN>

<SPAN
   id="BiometricApplication"
   style="
      top: 560;
      left: 47;
      width: 570px;
      height: 40px; 
      position: absolute;
      z-index: 1;
      background: grey;
      border-color: #000000;
      border-style: solid;
      border-top-width: 1px;
      border-bottom-width: 1px;
      border-left-width: 1px;
      border-right-width: 1px;
      text-align: left;
      color: white;
      font-family: Arial;
      font-size : 40;
      visibility: show;">
BIOMETRIC APPLICATION"
</SPAN>

<SPAN
   style="
      top: 650;
      left: 50;
      width: 570px;
      height: 40px; 
      position: absolute;
      z-index: 1;
      text-align: left;
      color: fa8b0f;
      font-family: Arial;
      font-size : 60;
      visibility: show;">
<script type="text/javascript"> document.write(computername());</script>
</SPAN>

<SPAN
   style="
      top: 670;
      left: 515;
      width: 570px;
      height: 40px;
      position: absolute;
      z-index: 1;
      text-align: left;
      color: white;
      font-family: Arial;
      font-face : Arial;
      font-size : 25;
      font-weight: bold;
      visibility: show;">
<font color="white"><PRE ID=myClock></font>
</SPAN>






</BODY> 
</HTML>

Open in new window

SOLUTION
Avatar of Emmanuel Adebayo
Emmanuel Adebayo
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rebel_no_1

ASKER

Thank you. This launches a .bat that I can use during testing. The final thing will most probably run a "compiled" .exe. May I ask so long, how would I run a Startup.exe instead of the Startup.bat?
I might be accused of moving the goal posts now.... :-)
Avatar of Bill Prew
Bill Prew

To run an EXE you do not need the CMD.EXE processor, that is just for BAT files.  So you should be able to do:

WshShell.Run """Startup.exe"""

If there are no spaces in the EXE path that you are specifying, then you can get away with:

WshShell.Run "Startup.exe"

Also, you likely want to wait for the EXE to finish before continuing, but maybe not.  The defauly is not to wait for it to finish, but there is a parm on the .RUN method to controil this, docs at https://msdn.microsoft.com/en-us/subscriptions/d5fk67ky%28v=vs.84%29.aspx

~bp
Also, it's your question to manage, but be aware there were some issues with the solution in http:#a41117746 that you also accepted.  I don't think the single quotes will work, and it started the wrong bat file name.  Also, I don't think it will work without either /C or /K option on the CMD execution.  Just clarifying for future readers, if you tested and it worked then let me know and I'll correct this post.

~bp
I am guilty. I did not test both solutions as I should've. I just tested the last one and went with your solution Bill. Then when I wanted to assign the points I realized there was an answer before you which to my untrained eye seemed to also be correct. I thought splitting the points would be fair but in hindsight it was not as the solution did not work. Therefore it's my mistake, sorry.
(Just for the record; there was a spelling mistake in your solution as well Bill) "Sheel" I corrected that before I tested.
@Rebel_no_1

Thanks, I edited the post, and noted the edit at the bottom, after the code, for benefit of future readers...

~bp