check if text file is empty....help

tinomesa

Member
Joined
Jul 14, 2003
Messages
13
Location
Maryland
Greetings,


I have a program that iterates through a series of text files and if the file size is 0 it writes to the file.

The problem is that I have text files that are empty but show up as being 1kb in size.....

Is there a way to find out if the file is empty preferably without the expense of "reading it" ?

Thanks,:D
 
You can use the Length property of FileInfo class:
Code:
Dim fi as new IO.FileInfo("path to the file")
MessageBox.Show(fi.Length.ToString())
If there is no text in the file the Length will be 0.
 
Thanks for the response Mutant,

But as I said above the file size is being reported as 1kb.

I think I am going to have to read a portion of the file an make a determination on wheater is null or not.......

Maybe one of you experts can tell me how "peek" works... and what it returns if there is nothing there to peek at.

Thanks
 
What I showed you shouldnt report 1kb on a empty file, it returns the numbers of bytes in the files, how many characters. If the file is empty then it will return 0.
 
Thanks for participating PlausiblyDamp,

the size is consistent at 2 Bytes,

It would work to check for size <= 2.......but how does the system diferenciate between 2kb and 2bytes.....

2KB maybe a correct file (not empty) that I do not want to overwrite.

any ideas?
 
mutant

How could I have doubted you..... Shame on me!!!

The length actually returns the number of bytes so you are

correct!!!

Seriously thanks to all for the help!!!
 
Explorer has a tendancy to round numbers up to the nearest 1K or 1M depending on size. The two bytes are probably a CRLF or something similar.
 
Back
Top