请帮助查看文件创建成功后无法打开查看的问题所在。
dim fname
fname=InputBox("Enter File Name:")
MsgBox("The File name is " & fname)
' Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const ForReading = 1
Const OpenAsASCII = 0
Const OverwriteIfExist = -1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fResultFile = objFSO.CreateTextFile(fname & ".txt", _
OverwriteIfExist, OpenAsASCII)
Set objInputFile = objFSO.OpenTextFile("iplist.txt", ForReading)
arrIpList = Split(objInputFile.ReadAll, vbCrLf)
objInputFile.Close
For l = 0 To UBound(arrIpList)
strIP = Trim(arrIpList(l))
If strIP <> "" Then
If Not IsConnectible(strIP, "", "") Then
fResultFile.WriteLine strIP & " is down"
End If
End If
Next
' Open Result file
Set inCsvSys = CreateObject("Scripting.FileSystemObject")
Set inCsv = inCsvSys.OpenTextFile(fname & ".txt")
WScript.Echo "FIle found and opened successfully"
Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Dim oShell, oFSO, sTempFile, fFile
If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName
oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True
Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)
Select Case InStr(fFile.ReadAll, "TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
fFile.Close
oFSO.DeleteFile(sTempFile)
End Function
根据文档,
OpenTextFile
方法这不是你想要做的。您需要运行一个程序并将文件名作为命令行参数传递给它。
尝试:
假设您的系统上为 txt 文件设置了默认应用程序,那么
.Run fname & ".txt"
(即,不指定应用程序)可能会起作用。