检查文件是否为空

Check if the file is empty

我正在使用一个 .hta,它在文本文件中包含一个单词列表当这个文件有 0kb 时就会出现问题...如果文件的大小为 0kb 我会怎么做而不是错误并且加载空白文件?

我的代码:

  <script type="text/vbscript">
Option Explicit
    Window.resizeTo 373,610
Const csFSpec = "MyList.TxT"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

Sub Window_OnLoad()
  If goFS.FileExists(csFSpec) Then
     document.all.DataArea.value = goFS.OpenTextFile(csFSpec).ReadAll()
  Else

     self.close
  End If

  If document.all.DataArea.value =vbcrlf Then

document.all.DataArea.value =""
Else

  End If
End Sub

Sub SaveFile()
If document.all.DataArea.value = "" Then
document.all.DataArea.value =vbcrlf
  goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value

     self.close
Else
document.all.DataArea.value = Replace(document.all.DataArea.value, "\", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "/", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, ":", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "*", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "?", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, """", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "<", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, ">", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "|", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "&", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "!", "_")
  goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value

  DIM objFSO
  DIM objFile
  DIM STRLINE
  DIM STRNEWCONTENTS

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("MyList.TxT", ForReading)

Do Until objFile.AtEndOfStream
    strLine = objFile.Readline
    strLine = Trim(strLine)
    If Len(strLine) > 0 Then
        strNewContents = strNewContents & strLine & vbCrLf
    End If
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile("MyList.TxT", ForWriting)
objFile.Write strNewContents
objFile.Close

     document.all.DataArea.value = goFS.OpenTextFile(csFSpec).ReadAll()
     document.all.DataArea.value = Replace(document.all.DataArea.value, vbcrlf,"|")
     document.all.DataArea.value = left(document.all.DataArea.value,len(document.all.DataArea.value)-1)
     document.all.DataArea.value = Replace(document.all.DataArea.value,"|",vbcrlf)
     goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value

     self.close
  End if
End Sub

Sub QuitEdit()

     self.close
End Sub

Sub Redefine()

document.all.DataArea.value ="CD" & vbcrlf & "Album" & vbcrlf & "Song" & vbcrlf & "DJ" & vbcrlf
End Sub

Sub Clean()

document.all.DataArea.value = ""

End Sub
  </script>

验证文件存在后测试.size(未测试):

If goFS.FileExists(csFSpec) Then
    SET objFile = goFS.GetFile(csFSpec)
    If objFile.Size > 0 Then
        document.all.DataArea.value = goFS.OpenTextFile(csFSpec).ReadAll()
    Else
        'Do something else
    End If
Else