batch-filecmd

GUI for running batch files


I have 3 batch files. One of them requires input from the user. I wonder how I can have a GUI with 3 buttons (1 for each batch file) and a textbox for user entry which will be used by one of the batch files?

I'm trying to use this answer to make it as shown below:

<!-- :: Batch section
@echo off
setlocal

echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
goto :EOF
-->


<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<body bgcolor="cyan">
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,100);

function closeHTA(reply){
   var fso = new ActiveXObject("Scripting.FileSystemObject");
   fso.GetStandardStream(1).WriteLine(reply);
   window.close();
}

</SCRIPT>
</HEAD>
<BODY>
   <button onclick="call C:\Users\user1\Documents\bat1.bat";closeHTA(1);>Bat1</button>
   <button onclick="call C:\Users\user1\Documents\bat2.bat;closeHTA(2);">Bat2</button>
   <button onclick="call C:\Users\user1\Documents\bat3.bat;closeHTA(3);">Bat3</button>
</BODY>
</HTML>

by I'm facing couple of problems:

  1. I get an error in line <button onclick="call C:\Users\user1\Documents\bat1.bat";closeHTA(1);>Bat1</button> saying a semicolon is expected as shown below, I'm not sure if this is the correct way to call a batchfile in this part? enter image description here
  2. How I can have a textbox so I can enter a parameter and pass it to one of the batch files?
  3. The GUI and cmd are not anchored so if I closed the cmd the GUI of the buttons still exists and doesn't close as shown below: enter image description here

Solution

  • Try like this:

    <!-- :: Batch section
    @echo off
    setlocal
    
    echo Select an option:
    for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
    echo End of HTA window, reply: "%HTAreply%"
    goto :EOF
    -->
    
    
    <HTML>
    <HEAD>
    <HTA:APPLICATION SCROLL="no" SYSMENU="no" >
    <body bgcolor="cyan">
    <TITLE>HTA Buttons</TITLE>
    <SCRIPT language="JavaScript">
    window.resizeTo(374,100);
    
    function closeHTA(reply){
       var fso = new ActiveXObject("Scripting.FileSystemObject");
       fso.GetStandardStream(1).WriteLine(reply);
       window.close();
    }
    
    function callShellApplication(command){
        var r = new ActiveXObject("WScript.Shell");
        var res=r.Exec(command);     
        new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(res.StdOut.ReadLine());   
        window.close();
    }
    
    
    
    </SCRIPT>
    </HEAD>
    <BODY>
       <button onclick='callShellApplication("C:\\test.bat")'>Bat1</button>
       <button onclick='callShellApplication("C:\\Users\\user1\\Documents\\bat2.bat")'>Bat2</button>
       <button onclick='callShellApplication("C:\\Users\\user1\\Documents\\bat2.bat")'>Bat3</button>
    </BODY>
    </HTML>
    

    When using javascript or html you'll need to double backslashes.You can't directly call a bat from HTA,but you need to use a WScript.Shell object.

    EDIT To pass arguments (like requested in the comments) you can try something similar to this:

    <!-- :: Batch section
    @echo off
    setlocal
    
    echo Select an option:
    for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
    echo End of HTA window, reply: "%HTAreply%"
    goto :EOF
    -->
    
    
    <HTML>
    <HEAD>
    <HTA:APPLICATION SCROLL="no" SYSMENU="no" >
    <body bgcolor="cyan">
    <TITLE>HTA Buttons</TITLE>
    <SCRIPT language="JavaScript">
    window.resizeTo(374,300);
    
    
    function sleepFor( sleepDuration ){
        var now = new Date().getTime();
        while(new Date().getTime() < now + sleepDuration){ /* do nothing */ } 
    }
    
    function callShellApplication(command){
        var args=document.getElementById('args').value;
        var r = new ActiveXObject("WScript.Shell");
        var res=r.Exec(command +" "+args);    
        var out="";
        while (res.Status == 0)
        {
             sleepFor(100);
        }
        while (!res.StdOut.AtEndOfStream){
            out=out+"\r\n"+res.StdOut.ReadLine();
        }
    
        var StS=new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
        StS.Write(out);     
        //new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(res.StdOut.ReadLine());   
        window.close();
    }
    
    
    
    </SCRIPT>
    </HEAD>
    <BODY>
       Aruments:<textarea name="args" cols="40" rows="1"></textarea>
       <hr>
       <button onclick='callShellApplication("C:\\test.bat")'>Bat1</button>
       <button onclick='callShellApplication("C:\\Users\\user1\\Documents\\bat2.bat")'>Bat2</button>
       <button onclick='callShellApplication("C:\\Users\\user1\\Documents\\bat2.bat")'>Bat3</button>
    </BODY>
    </HTML>