How to check if a file is empty

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2009
Posts: 27
Reputation: Tamir09 is an unknown quantity at this point 
Solved Threads: 0
Tamir09 Tamir09 is offline Offline
Light Poster

How to check if a file is empty

 
0
  #1
Feb 22nd, 2009
Ok, I know about how to check for the end of a file, but what I need to know, is if you create a new file, thats empty, how do you check if it really is empty, and if its the same as EOF, then what am I doing wrong with this code here:
  1.  
  2. If Dir(My.Application.Info.DirectoryPath & "\TurbineData.xml") <> "" Then
  3.  
  4. If (TextBox1.Text.Equals("") Or TextBox2.Text.Equals("") Or TextBox3.Text.Equals("") Or TextBox4.Text.Equals("")) Then
  5.  
  6. Dim title2 = "Invalid Values"
  7. MsgBox(" Form contains no values to save.", , title2)
  8.  
  9. Else
  10. Dim ExistingData As New MyDatum
  11.  
  12. Dim Serializer As New System.Xml.Serialization.XmlSerializer(GetType(MyDatum))
  13.  
  14. If (File.Exists(My.Application.Info.DirectoryPath & "\TurbineData.xml" & Not EOF(1))) Then
  15.  
  16. Dim OpenStream As System.IO.FileStream = System.IO.File.Open(My.Application.Info.DirectoryPath & "\TurbineData.xml", IO.FileMode.Open)
  17.  
  18. ExistingData = Serializer.Deserialize(OpenStream)
  19.  
  20. OpenStream.Close()
  21. Else
  22. Dim err05 = "File does not Exist"
  23. MsgBox("Creating new XML file.", , err05)
  24.  
  25. End If
  26.  
  27. Dim NewData As New MyData
  28.  
  29. TextBox1.Focus()
  30. NewData.DateStamp = CDate(Date.Today()) 'DateValue(Now)
  31. NewData.TimeStamp = CDate(TimeString())
  32. NewData.Gap = gaptext.Text
  33.  
  34. ExistingData.Add(NewData)
  35.  
  36. Dim SaveStream As System.IO.FileStream = System.IO.File.Open(My.Application.Info.DirectoryPath & "\TurbineData.xml", IO.FileMode.Create)
  37.  
  38. Serializer.Serialize(SaveStream, ExistingData)
  39.  
  40. SaveStream.Close()
  41.  
  42. Dim title3 = "Save Alert"
  43. MsgBox("Data Saved to " & My.Application.Info.DirectoryPath & " \TurbineData.xml", , title3)
  44.  
  45. End If
  46.  
  47. Else
  48. Dim err05 = "File does not Exist"
  49. MsgBox("Creating new XML file.", , err05)
  50.  
  51. Dim SaveStream As System.IO.FileStream = System.IO.File.Open(My.Application.Info.DirectoryPath & "\TurbineData.xml", IO.FileMode.Create)
  52. SaveStream.Close()
  53. End If

Thats my code there, and the idea of this is that if the file is empty, then create a new entry in the xml, if the file already has data in it, then add to the file, dont overwrite the data.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: How to check if a file is empty

 
0
  #2
Feb 22nd, 2009
Ah.... You are posting VB.NET code in the VB 4/5/6 Forum.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 27
Reputation: Tamir09 is an unknown quantity at this point 
Solved Threads: 0
Tamir09 Tamir09 is offline Offline
Light Poster

Re: How to check if a file is empty

 
0
  #3
Feb 23rd, 2009
Oh I didnt know there was a difference...thought it was the same forum. I wonder if anyone has a solution to this?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 28
Reputation: edgar5 is an unknown quantity at this point 
Solved Threads: 2
edgar5 edgar5 is offline Offline
Light Poster

Re: How to check if a file is empty

 
0
  #4
Feb 23rd, 2009
  1. Private Sub RenameFile(ByRef fileNameFull As String)
  2. Dim fileI As FileInfo = New FileInfo(fileNameFull)
  3. If fileI.Exists Then
  4. Dim binReader As New BinaryReader(File.Open(fileNameFull, FileMode.Open))
  5. Dim fileClosed As Boolean = False
  6. Try
  7. ' read the first 4 bytes into a buffer to
  8. ' determine if the file is empty.
  9. Dim testArray As Byte() = {0, 0, 0, 0}
  10. Dim count As Integer = binReader.Read(testArray, 0, 3)
  11. If count <> 0 Then 'file NOT empty
  12. ' Reset the position in the stream to zero.
  13. binReader.BaseStream.Seek(0, SeekOrigin.Begin)
  14. 'do append stuff
  15. Else
  16. 'file empty
  17. End If
  18. Catch ex As Exception
  19. If Not fileClosed Then
  20. binReader.Close()
  21. fileClosed = True
  22. End If
  23. MsgBox("Uncaught general Exception error. " + ex.GetType().Name + vbNewLine + ex.Message)
  24. Finally
  25. If Not fileClosed Then
  26. binReader.Close()
  27. fileClosed = True
  28. End If
  29. End Try
  30. End If
  31. End Sub
-Ed
MS Vista Ultimate 64-bit; Visual Studio 2008 & 2010 beta; Visual Basic; Visual C++

Asus P5E3 Deluxe w/ Intel E6850, 8 GB OCZ DDR3 PC3-12800, SAPPHIRE HD3870 GPU
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 51
Reputation: martonx is an unknown quantity at this point 
Solved Threads: 8
martonx martonx is offline Offline
Junior Poster in Training

Re: How to check if a file is empty

 
0
  #5
Feb 24th, 2009
  1. Dim reader() As String
  2. reader = File.ReadAllLines(spath)
  3. length = reader.GetLength(0)

If length=0 then your file is empty. I didn't checked this method with .xml, but with .txt it is working.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC