How to send values from hta file to a batch file using Vbscript?
- We validated Database name (only alphanumeric, underscore and dollar allowed) and password (not space and tab allowed) using regular expression..
- We used Folder Dialog box to select the required folder.
- Send values from Hta file to Batch file
- Remove double quotes of the value obtained from Hta file.
Step 1 : Create a hta file named Installer .hta file.
< html >
< head >
Note: Setting the attributes of a Hta application
< hta:application
border="thick"
MAXIMIZEBUTTON="no"
borderstyle="normal"
caption="true"
contextmenu="false"
icon="Icon_html.JPG"
showintaskbar="true"
singleinstance="true"
sysmenu="true"
version="1.0"
WINDOWSTATE="normal" >
< script language="vbscript" >
Dim Master
Dim Master1
Note: Resize and move the window to center of the screen
Sub Window_onLoad
CenterWindow 790, 520
End Sub
Sub CenterWindow( widthX, heightY )
self.MoveTo (screen.Width - widthX)/2,(screen.Height - heightY)/2
self.ResizeTo widthX, heightY
End Sub
Note: Folder Dialog Browser
Sub ChooseSaveFolder
strStartDir = ""
userselections.txtFile.value = PickFolder(strStartDir)
End Sub
Sub ChooseSaveFolder1
strStartDir = ""
userselections.txtFile1.value = PickFolder(strStartDir)
End Sub
Function PickFolder(strStartDir)
Dim SA, F
Set SA = CreateObject("Shell.Application")
Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not F Is Nothing) Then
PickFolder = F.Items.Item.path
End If
Set F = Nothing
Set SA = Nothing
End Function
Sub CheckMaster
Note : Database name and Password Validation
Dim regEx
Dim DBName
DBNAme=Trim(userselections.txtFile2.value)
if Not DBNAme="" then
Set regEx = New RegExp
regEx.IgnoreCase = False
regEx.Pattern ="^[a-zA-Z0-9_$]+$"
if Not regEx.Test(DBName) then
msgbox " Please enter valid Database Name "
Exit Sub
End If
End If
Dim regEx1
Dim DBPwd
DBPwd=Trim(userselections.txtFile3.value)
if Not DBPwd="" then
Set regEx1 = New RegExp
regEx1.IgnoreCase = False
regEx1.Pattern = "^[ ^\s ]+$"
if Not regEx1.Test(DBPwd) then
msgbox " Please enter valid Database Password "
Exit Sub
End If
End If
If userselections.txtFile.value ="" OR userselections.txtFile1.value ="" OR userselections.txtFile2.value ="" OR userselections.txtFile3.value ="" Then
msgbox "Please Enter the Installer Plugin details!!!"
Else
Note : Sending data to Batch file "Installer.bat"
Dim arrLogin
arrLogin = Array(userselections.txtFile.value,userselections.txtFile1.value,userselections.txtFile2.value,userselections.txtFile3.value)Set WShell = CreateObject("Wscript.Shell")
Wshell.run "Installer.bat """ & arrLogin(0) & """ """& arrLogin(1) &""" """& arrLogin(2) &""" """& arrLogin(3) &""""
Set WShell = Nothing
window.close
End If
End Sub
< /script >
< /head >
< body background="PlugIn.JPG" >
< form name="userselections" >
< br > < br > < br > < br > < br > < br > < br >
< table BORDER=10 BORDERCOLORLIGHT=Lightblue BORDERCOLORDARK=BLUE align=center >< tr >
< td >
WAMP Server Installation Directory
< /td >
< td >
< input type = "text" name = "txtFile" size="40"/ >
< input type = "button" value = "Browse..." onClick="ChooseSaveFolder" style="height:25px; width:100px; color:blue"/ >
< /td >
< /tr >
< tr >
< td >
Test case Management Tool Installation Directory
< /td >
< td >
< input type = "text" name = "txtFile1" size="40"/ >
< input type = "button" value = "Browse..." onClick="ChooseSaveFolder1" style="height:25px; width:100px; color:blue"/ >
< /td >
< /tr >
< tr >
< td >
Test case Management Tool Database Name
< /td >
< td >
< input type = "text" name = "txtFile2" size="40"/ >< /td >
< /td >
< /tr >
< tr >
< td >
Test case Management Tool Database Password
< /td >
< td >
< input type = "password" name = "txtFile3" size="40"/ >
< /td >
< /tr >
< tr align=center >
< td colspan=2 >
< input type="button" value="Submit" name="run_button" onClick="CheckMaster" style="height:25px; width:150px; color:blue"/ >
< /td >
< /tr >
< /table >
< /form >
< /body >
< /html >
Step 2 : Create a bat file named Installer. bat
This file obtains values from hta file with double quotes. Here we are removing that double quotes with the help of for loop
@ECHO off
ECHO.
set wp_dir= %1
Note :Remove double quotes
for /f "useback tokens=*" %%a in ('%wp_dir%') do set wp_dir=%%~a
SET tl_dir= %2
for /f "useback tokens=*" %%a in ('%tl_dir%') do set tl_dir=%%~a
SET tl_dbname= %3
for /f "useback tokens=*" %%a in ('%tl_dbname%') do set tl_dbname=%%~a
SET tl_db_passwd= %4
for /f "useback tokens=*" %%a in ('%tl_db_passwd%') do set tl_db_passwd=%%~a
ECHO WAMP Server Installation Directory: %wp_dir%
ECHO Test case management tool Installation Directory: %tl_dir%
ECHO Test case management tool Database name: %tl_dbname%
PAUSE
This post first appeared on Every Day Of Your Life Is A Page Of Your History, please read the originial post: here