Simple HTA Template to Run Utility on File


Many nice tools are only command line based, and sometimes they do not have a GUI versions. Command-line tools are fine for computer geeks but if you are one of the users that hate the black&white console, and this might be useful to you.

Let’s say, if you have a source file which is process.exe that takes a single parameter which is a *.csv file and if would be nice to have a simple GUI like this:

hta-file-utility Simple HTA Template to Run Utility on File HTA vbscript windows command shell windows scripting host

hta-file-utility

Users then can browse and select a file, press “Process” and the GUI automatically invokes the correct command line tool. The process.exe (or whatever tool) should be present in the same folder of the GUI.

Of course, you can choose fancy programming language e.g. C# to do this. But, what if that you don’t need to download Visual Studio, and you don’t need to compile the source code, or download any other language packs. Windows have inbuilt support for *.HTA (See this post and this).

HTA are made for this purpose. It combines the advantages of both *.vbs/*.js and *.html. It is inherently supported on Windows platform (but it is not easy to migrate to other platforms). Double click the *.hta will invoke the mshta.exe to interprete and run the HTML application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<html>
<head>
<title>File Utility</title>
<HTA:APPLICATION
    APPLICATIONNAME="File Utility"
    ID="hta_utility"
    border            = "thin"
    borderStyle        = "tool"
    caption            = "yes"
    contextMenu        = "no"
    maximizeButton    = "no"
    minimizeButton    = "yes"
    navigable        = "no"
    showInTaskbar    = "yes"
    singleInstance    = "yes"
    scroll = "no"
    sysmenu            = "yes"
    windowState        = "normal"  
    VERSION="1.0"/>
</head>
 
<script language="VBScript">
Dim SourceDir, FullName, arrFN
Dim ToolFileName
Dim CommandFile
Const AllowFileExt = "CSV"
Const Width = 500
Const Height = 130
 
' get the directory of the HTA
FullName = Replace(hta_utility.commandLine, Chr(34), "")  
arrFN = Split(FullName, "\")  
FileName = arrFN(Ubound(arrFN))  
SourceDir = Replace(FullName, FileName, "")  
 
Function FileExists(filename)
    Set fso = CreateObject("Scripting.FileSystemObject")    
    If (fso.FileExists(filename)) Then
       FileExists = True
    Else
       FileExists = False
    End If
    Set fso = Nothing
End Function
 
Sub DoResize
    'resize   
    window.resizeTo Width, Height
    screenWidth = Document.ParentWindow.Screen.AvailWidth
    screenHeight = Document.ParentWindow.Screen.AvailHeight
    posLeft = (screenWidth - Width) / 2
    posTop = (screenHeight - Height) / 2     
    'move to centerscreen
    window.moveTo posLeft, posTop
End Sub
  
Sub Window_OnLoad
  DoResize
  ' set tool file paths
  ToolFileName = SourceDir & "CSV.vbs"
  CommandFile = "cscript.exe" & " " & Chr(34) & ToolFileName & Chr(34)
End Sub
 
' process the file
Sub OnClickButtonButton1()
  If FileExists(ToolFileName) Then
    If FileExists(file.value) Then
        Set fso = CreateObject("Scripting.FileSystemObject")    
        If (UCase(fso.GetExtensionName(file.value)) = AllowFileExt) Then
            Set obj = CreateObject("WScript.Shell")
            obj.Run CommandFile & " " & Chr(34) & file.value & Chr(34), 0, True
            MsgBox "Done"
            Set obj = Nothing
        Else
            MsgBox ToolFileName & " is not " & AllowFileExt
        End If          
        Set fso = Nothing
    Else
        MsgBox "File Not Found: " & file.value
    End If
  Else
    MsgBox ToolFileName & " not found!"
  End If
End Sub
</script>
 
<body>
    <input type="file" name="file" value="file"/>
    <input type="button" name="Button1" id="Button1" value="Process" onclick="OnClickButtonButton1">
</body>
</html>
<html>
<head>
<title>File Utility</title>
<HTA:APPLICATION
	APPLICATIONNAME="File Utility"
	ID="hta_utility"
    border            = "thin"
    borderStyle        = "tool"
    caption            = "yes"
    contextMenu        = "no"
    maximizeButton    = "no"
    minimizeButton    = "yes"
    navigable        = "no"
    showInTaskbar    = "yes"
    singleInstance    = "yes"
    scroll = "no"
    sysmenu            = "yes"
    windowState        = "normal"  
	VERSION="1.0"/>
</head>

<script language="VBScript">
Dim SourceDir, FullName, arrFN
Dim ToolFileName
Dim CommandFile
Const AllowFileExt = "CSV"
Const Width = 500
Const Height = 130

' get the directory of the HTA
FullName = Replace(hta_utility.commandLine, Chr(34), "")  
arrFN = Split(FullName, "\")  
FileName = arrFN(Ubound(arrFN))  
SourceDir = Replace(FullName, FileName, "")  
 
Function FileExists(filename)
	Set fso = CreateObject("Scripting.FileSystemObject")	
	If (fso.FileExists(filename)) Then
	   FileExists = True
	Else
	   FileExists = False
	End If
	Set fso = Nothing
End Function

Sub DoResize
    'resize   
    window.resizeTo Width, Height
    screenWidth = Document.ParentWindow.Screen.AvailWidth
    screenHeight = Document.ParentWindow.Screen.AvailHeight
    posLeft = (screenWidth - Width) / 2
    posTop = (screenHeight - Height) / 2     
    'move to centerscreen
    window.moveTo posLeft, posTop
End Sub
  
Sub Window_OnLoad
  DoResize
  ' set tool file paths
  ToolFileName = SourceDir & "CSV.vbs"
  CommandFile = "cscript.exe" & " " & Chr(34) & ToolFileName & Chr(34)
End Sub

' process the file
Sub OnClickButtonButton1()
  If FileExists(ToolFileName) Then
  	If FileExists(file.value) Then
  		Set fso = CreateObject("Scripting.FileSystemObject")	
  		If (UCase(fso.GetExtensionName(file.value)) = AllowFileExt) Then
  			Set obj = CreateObject("WScript.Shell")
  			obj.Run CommandFile & " " & Chr(34) & file.value & Chr(34), 0, True
  			MsgBox "Done"
  			Set obj = Nothing
  		Else
  			MsgBox ToolFileName & " is not " & AllowFileExt
  		End If  		
  		Set fso = Nothing
  	Else
  		MsgBox "File Not Found: " & file.value
  	End If
  Else
    MsgBox ToolFileName & " not found!"
  End If
End Sub
</script>

<body>
	<input type="file" name="file" value="file"/>
	<input type="button" name="Button1" id="Button1" value="Process" onclick="OnClickButtonButton1">
</body>
</html>

Make sure you change the variables in Window_OnLoad to set the correct command line tool and you will need to set the correct AllowFileExt as well.

Finally, you can use this tool to protect your HTA source code and make them *.EXE executables (32-bit or 64-bit).

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
699 words
Last Post: How to Run HTA as Administrator (Elevation)?
Next Post: How to Create UUID in PHP?

The Permanent URL is: Simple HTA Template to Run Utility on File

Leave a Reply