Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hide CMD prompt on HTA Script

91 views
Skip to first unread message

CleberMarques.com

unread,
May 29, 2008, 12:46:03 PM5/29/08
to
Hi Guys,
How can I hide the CMD screen when I execute this HTA script?

<html>
<head><title>Traceroute</title>
<HTA:APPLICATION ID="oHTA";
APPLICATIONNAME="Traceroute";
BORDER="thin";
BORDERSTYLE="normal";
SINGLEINSTANCE="no";
>
</head><body bgcolor="#E8E8E8" >
<font size=2 face="Century Gothic, Tahoma, Arial" color="black">
<script language="VBScript" type="text/vbscript">
set objShell = CreateObject("WScript.Shell")
strOut=""
sub traceroute
cmdarg="%comspec% /c tracert.exe " & T1.value
set objExCmd = objShell.Exec(cmdarg)
strOut=objExCmd.StdOut.ReadAll
Set regEx = New RegExp
regEx.Pattern = "[\f\n\r\v]+"
regEx.Global = True
regEx.Multiline = True
strOut = regEx.Replace(strOut, "<br>")
TraceOut.innerHTML= strOut
end sub
//-->
</script>
<p><b>Traceroute</b><hr noshode color="#000000"><br>
<p>Hostname: <input type="text" size="40" name="T1">
<input type="submit" name="B1" value="Submit" onclick="traceroute"></p>
<div id=TraceOut></div>
<script language="JavaScript">
<!--
if (window.resizeTo) self.resizeTo(600,400);
//-->
</script>

Tom Lavedas

unread,
May 29, 2008, 1:41:14 PM5/29/08
to
On May 29, 12:46 pm, CleberMarques.com <cont...@clebermarques.com>
wrote:

If this were a standalone VBS script, there is a way to do that with a
reentrant procedure, but it's complicated and messy. My advice is
that the easiest way is to convert the application to Run from Exec.

I use the following function to do the job ...

Function CmdPrompt(sCmd)
Dim alines, sCmdLine, stemp, ofs, oWS, nRes
'On Error Resume Next
sCmdLine = """%comspec%"" /c " & sCmd & " 1>> "
set ofs = CreateObject("Scripting.FileSystemObject")
stemp = ofs.GetTempName
set oWS = CreateObject("Wscript.Shell")
stemp = oWS.Environment("PROCESS")("TEMP") & "\" & stemp
nRes = oWS.Run(sCmdLine & Chr(34) & sTemp & Chr(34) _
, Abs(cSng(bConsoleSw)), True)
' optional
' alines = "ERRORLEVEL: " & nRes & vbCRLF
if ofs.FileExists(sTemp) Then
with ofs.OpenTextFile(stemp)
if Not .AtEndofStream Then
alines = aLines & .ReadAll
End if
End With
ofs.DeleteFile stemp
alines = Split(aLines, vbNewline)
Else
aLines = Array(nRes, "")
End if
ReDim Preserve alines(Ubound(alines) - 1)
if Err.Number <> 0 Then _
aLines = Array("Error Number:" & CStr(Err.Number),
Err.Description)
CmdPrompt = alines
End Function

It delivers an array of the output lines of text from the console
application. It returns an array with only one element, containing
the err or errorlevel, if the operation failed.

In your application, add the function's code and it could be applied
thus ...

Replace ...

set objExCmd = objShell.Exec(cmdarg)
strOut=objExCmd.StdOut.ReadAll

with

strOut=Join(CmdPrompt(cmdar), "<br>"))

and the regular expression should be unnecessary (I think) - I'm not
into regular expressions enough to be certain I grok exactly what it
is doing - beside replacing newlines with the <br> HTTP tag.

OK, I reviewed the documentation - it will miss formfeeds, returns
without linefeeds and vertical tabs - but I haven't seen too many of
those issued from MS console apps.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

ekkehard.horner

unread,
May 29, 2008, 2:54:26 PM5/29/08
to
Tom Lavedas schrieb:
[...]

> nRes = oWS.Run(sCmdLine & Chr(34) & sTemp & Chr(34) _
> , Abs(cSng(bConsoleSw)), True)
[...]

Nice script, Tom. Just one plea: Could you explain the magic
of "Abs(cSng(bConsoleSw))"?

Thanks

Tom Lavedas

unread,
May 29, 2008, 6:02:17 PM5/29/08
to
On May 29, 2:54 pm, "ekkehard.horner" <ekkehard.hor...@arcor.de>
wrote:

Sorry, I can't - it's a secret ;^))

That bit of code is a holdover from a previous version where I tried
to add a logic switch that would show the console for testing
purposes. What it does is convert the -1 of a logic True to a
nWindowType value of 1 and a logic False would evaluate as a zero.
For example, ...

for each bConsoleSw in array(true, false)
wsh.echo cstr(bConsoleSw), Abs(cSng(bConsoleSw))
next

I never noticed that I failed to remove it when I puled out the other
part that changed the COMSPEC switch from a toggle between /C and /K.
Just replace it with zero.

0 new messages