Check if the file is empty

Humberto Freitas picture Humberto Freitas · Jun 23, 2015 · Viewed 9.1k times · Source

I'm using a .hta which carries a list of words in a text file the problem occurs when this file has 0kb... how I would do so if the file has the size 0kb not of an error and load the blank file?

My Code:

  <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>

Answer

David picture David · Jun 23, 2015

Test .size after verifying file existence (not tested):

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