使用VB脚本打开HTML按钮的本地驱动器中的日志文件

时间:2014-05-30 13:32:19

标签: html vbscript

我想在(C:驱动器位置中的本地计算机中打开日志文件(test.log) VB脚本函数说" LogGen" 。这个日志文件应该在点击按钮,VB脚本函数" LogGen"应该被调用并执行。

<input type="button" name="Log" id="Start" value="  Login  " onclick="LogGen">

我是VB脚本的新手。有人可以建议我使用Vb脚本函数的一些示例代码来获取本地目录中的日志文件吗?


新问题代码


Sub LogOpen()
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, TextLine
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file for output.
FileName = "c:\\testfile.txt"
  Set MyFile = fso.OpenTextFile(FileName, ForWriting, True, TristateTrue)
' Open the file for input.
 Set MyFile = fso.OpenTextFile(FileName, ForReading)
' Read from the file and display the results.
 Do While MyFile.AtEndOfStream <> True
     TextLine = MyFile.ReadLine
     Document.Write TextLine & "<br />"
 Loop
'MyFile.Close
End Sub

我做了如上所示的那个,并在一个名为&#34; Log&#34;的按钮事件中调用了这个子程序。但它不起作用。 出现yz类型的空白屏幕,它占据整个窗口,然后必须关闭应用程序。 请问你可以建议错误的位置。我希望点击“日志”按钮时弹出C目录中的日志文件,但这不会发生。

<input type="button" name="Log" value="Log" onclick="LogOpen"><br/>

2 个答案:

答案 0 :(得分:3)

您可以查看此代码,以便了解您想要做什么:

<html>
<Title>How to open and read the log file with HTA</Title>
<head>
<HTA:APPLICATION 
ICON="cmd.exe"
APPLICATIONNAME = "How to open and read the log file with HTA" 
BORDER="dialog"
BORDERSTYLE="complex"
WINDOWSTATE="maximize"
>
<style>
body{
background-color: Black;
}
</style>
</head>
<script type="text/Vbscript">
Option Explicit
Dim File
File = "C:\MyLogFile.txt"
'***********************************************************
Sub LoadMyFile()
    myDiv.innerHTML = LoadFile(File)
End Sub
'***********************************************************
Function LoadFile(File)
    On Error Resume Next
    Dim fso,F,ReadMe,Tab,i,paragraphe
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set F = fso.OpenTextFile(File,1)
    LoadFile = Err.Number
    If Err.Number <> 0  Then
        MsgBox Err.Description,16," Error"
        Exit Function
    End If
    ReadMe = F.ReadAll
    Tab = split(ReadMe,vbcrlf)
    For i = lbound(Tab) to ubound(Tab)
        paragraphe=paragraphe & Tab(i) & "<br>"
    Next
    LoadFile = paragraphe
End Function
'***********************************************************
Sub Clear()
    myDiv.innerHTML = ""
End Sub
'***********************************************************
Function LogOpen()
    Dim Ws,iReturn,strError
    Set ws = CreateObject("WScript.Shell")
    On Error Resume Next
    iReturn = Ws.Run(File,1,False)
    If Err.Number <> 0  Then
        strError = "<b><font color=Red>The file "& File &" dosen't exists !</font></b>"
        myDiv.InnerHTML = strError
        Exit Function
    End If
End Function
'***********************************************************
</script>
<body text="white">
<center><input type="button" name="Log" id="Start" value="  Load LogFile  " onclick="LoadMyFile()">
<input type="button" name="Log" id="Start" value="  Open LogFile  " onclick="LogOpen()">
<input type="button" value="  Clear  " onclick="Clear()"></center>
<Div id="myDiv"></Div>
</body>
</html>

答案 1 :(得分:1)

您可以采取以下措施:

Const ForReading = 1
set fs = CreateObject("Scripting.FileSystemObject")
set objTextFile = fs.OpenTextFile(filePathto/test.log, ForReading)

这里有一个更完整的描述:vbs open file