检查文件是否为空

时间:2015-06-23 00:12:24

标签: vbscript hta

我使用.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>

1 个答案:

答案 0 :(得分:6)

在验证文件存在(未测试)后测试.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