2

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
Tak
  • 3,536
  • 11
  • 51
  • 93

1 Answers1

2

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>
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Thank you very much. This solved the first problem. Any advice regarding having an input textbox to pass parameters to a batch file? and also is there a way to anchor the GUI to the cmd so that it can be dragged and floating around? – Tak Apr 09 '18 at 09:58
  • @Tak - for arguments - check my edit.As for the your second question - I'm not sure what you are asking exactly. You want system menu for ye hta window? – npocmaka Apr 09 '18 at 10:07
  • Thank you. And how I can pass the argument to the called batch file. I did this `` and in test.bat I did this I wrote this in the beginning SET "arg1=%1" and used arg1. But then when I run the batch file, enter the argument and click the button1 to run test.bat, the batch file close and nothing happens. Any advice what I'm doing wrong? – Tak Apr 09 '18 at 10:27
  • @Tak - is your set inside brackets? What have you wrote in the input textfield? I've tested this with a single line batch file : `@echo ##%1##` and it worked fine. – npocmaka Apr 09 '18 at 10:36
  • It was inside brackets then I removed it now but still not working. In the textfield I wrote a string something like user1 . Am I correctly passing args here correctly `` ? – Tak Apr 09 '18 at 10:52
  • @Tak - try the bat directly with your arguments to see what happens.It could be an issue with the bat itself. – npocmaka Apr 09 '18 at 11:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168556/discussion-between-tak-and-npocmaka). – Tak Apr 09 '18 at 11:50
  • The batch file was already working. The only change I did in the test.bat was changing SET "username=user1" to SET username=%1 – Tak Apr 09 '18 at 11:52
  • I've just tested the test.bat by calling it from the cmd (not the GUI batch file) using `call "C:\\test.bat" user1` and worked successfully. So this means the problem is in the GUI batch file calling the test.bat. Am I correctly passing args correctly in the GUI batch `` ? – Tak Apr 09 '18 at 11:57
  • oh may be because my path has spaces? like the original location of test.bat is "C:\\my folder\test.bat" – Tak Apr 09 '18 at 12:05
  • I am currently facing something strange. When I run it on windows10 it works fine, but when I try win7 bat1 file contains a line calling powershell script, the script is executed successfully but then it is paused and the rest of the batch file is not executed. If I run the bat1 file from directly not using this GUI way it works fine, so looks like it's something to do with the GUI. Any advice please? the code of the bat1 file and powershell script can be found here https://pastebin.com/qxQEgyA8 – Tak Jun 12 '18 at 09:21
  • Did you get the time to check it? – Tak Jun 13 '18 at 07:01
  • @Tak - i cant import the active directory module – npocmaka Jun 14 '18 at 07:54
  • Ah okay, but this part is working fine with me, like the user is added to the correct group then the displayname is saved to the text file. The problem is that after the powershell is executed the batch file stops working, and this only happens on win7, it works fine on win10, so I'm confused, any thoughts? – Tak Jun 14 '18 at 07:57
  • @Tak - unfortunately I have no win7 machine to test the script. Nothing comes to me by just checking the code. – npocmaka Jun 14 '18 at 08:19