如何纠正VBscript运行时错误:输入文件末尾

时间:2015-09-03 01:24:08

标签: vbscript

我为此代码收到以下错误。请问你能告诉它哪里错了吗?第71行是“urls2 = objInputFile.ReadAll”。

第71行 人物1 错误:输入文件的结尾 代码:800A003E 来源:Microsoft VBScript运行时错误。

inputfile = "C:\Evernote.html"
outputfolder = "c:\"


msgbox("launched. press ok to continue")


'create urls1.txt
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(outputfolder & "urls1.txt", TRUE)

'read inputfile (evernote exported html) 
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objInputFile = objFileSystem.OpenTextFile(inputfile, 1)
html = objInputFile.ReadAll
objInputFile.Close


'split html var
html = Split(html, "<tr><td><b>Source:</b></td><td><a href=""")

'loop through html array and clean up the results so you get just the urls
'and write them to urls1.txt
For i = 1 To UBound(html)
checkA = InStr(html(i), """")
    if checkA > 1 then
        html(i) = Split(html(i), """")
        urls = html(i)(0)
        objOutputFile.WriteLine(urls)
    end if
Next



'remove duplicates

'create urls2.txt
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(outputfolder & "urls2.txt", TRUE)

'read urls1.txt and remove duplicates and write results to urls2.txt
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = outputfolder & "urls1.txt"
Set objFile = objFS.OpenTextFile(strFile)
Set d = CreateObject("Scripting.Dictionary")
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If Not InStr(strLine,"--------") >0 Then
    If Not d.Exists(strLine) Then 
        d.Add strLine , 0
    End If 
End If 
Loop
x=d.Items
For Each strKey In d.keys
objOutputFile.WriteLine(strKey)
Next



'sort alphabetically



'read urls2.txt and sort everything alphabetically


'read urls2.txt
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objInputFile = objFileSystem.OpenTextFile(outputfolder & "urls2.txt", 1)
urls2 = objInputFile.ReadAll
objInputFile.Close

'split each line into array
urls2 = Split(urls2, VBCrLf)

'sort urls2 array by alphabet with bubble sort method

For i = (UBound(urls2) - 1) to 0 Step -1
For j= 0 to i
    If UCase(urls2(j)) > UCase(urls2(j+1)) Then
        strHolder = urls2(j+1)
        urls2(j+1) = urls2(j)
        urls2(j) = strHolder
    End If
Next
Next

'write the sorted version of urls2.txt in urlsfinal.txt

'create urlsfinal.txt
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(outputfolder & "urlsfinal.txt", TRUE)

'write all sorted vars from urls2 array to urlsfinal.txt
For i = 0 to UBound(urls2)
objOutputFile.WriteLine(urls2(i))
next



msgbox("all done")

1 个答案:

答案 0 :(得分:2)

问题是您的源文件urls1.txt为空。原因是您在写入文件后没有关闭文件。完成写到urls2.txt objOutputFile.Close 后,您需要添加此内容。

objFileSystem

此外,每次访问文件时,您都不需要不断重新创建set的实例。您可以在顶部实例化一次。

确保成为一名优秀的记忆公民并销毁代码中Set objFileSystem = Nothing 的所有对象。

{{1}}