Directory / File IO / Registry Topics include: IO, file system and the registry

Go Back  Xtreme .NET Talk > .NET > Directory / File IO / Registry > check if text file is empty....help


Reply
 
Thread Tools Display Modes
  #1  
Old 08-12-2003, 10:27 AM
tinomesa tinomesa is offline
Newcomer

Preferred language:
vb.net
 
Join Date: Jul 2003
Location: Maryland
Posts: 13
tinomesa is on a distinguished road
Cool check if text file is empty....help



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,
Reply With Quote
  #2  
Old 08-12-2003, 10:47 AM
mutant mutant is offline
Ultimate Contributor

Preferred language:
Any language
 
Join Date: Jan 2003
Location: Enfield, CT, USA
Posts: 1,771
mutant is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 08-12-2003, 11:27 AM
tinomesa tinomesa is offline
Newcomer

Preferred language:
vb.net
 
Join Date: Jul 2003
Location: Maryland
Posts: 13
tinomesa is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 08-12-2003, 11:38 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Sep 2002
Location: Lancashire, UK
Posts: 6,463
PlausiblyDamp is on a distinguished road
Default

if you bring up the property page in explorer for one of these 1k but empty files what does it say about the file size there?
__________________
Posting Guidelines FAQ Post Formatting

Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein
Reply With Quote
  #5  
Old 08-12-2003, 11:40 AM
mutant mutant is offline
Ultimate Contributor

Preferred language:
Any language
 
Join Date: Jan 2003
Location: Enfield, CT, USA
Posts: 1,771
mutant is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 08-12-2003, 11:50 AM
tinomesa tinomesa is offline
Newcomer

Preferred language:
vb.net
 
Join Date: Jul 2003
Location: Maryland
Posts: 13
tinomesa is on a distinguished road
Default

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?
Reply With Quote
  #7  
Old 08-12-2003, 12:00 PM
tinomesa tinomesa is offline
Newcomer

Preferred language:
vb.net
 
Join Date: Jul 2003
Location: Maryland
Posts: 13
tinomesa is on a distinguished road
Default

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!!!
Reply With Quote
  #8  
Old 08-13-2003, 06:01 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Sep 2002
Location: Lancashire, UK
Posts: 6,463
PlausiblyDamp is on a distinguished road
Default

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.
__________________
Posting Guidelines FAQ Post Formatting

Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Move focus when text field left empty? Mike521 ASP.NET 4 07-01-2004 10:20 AM
Check if text is numeric... Nick_Net2004 General 7 05-14-2004 09:23 AM
Check for empty text in textbox Malfunction Windows Forms 2 02-27-2004 05:43 AM
Check text ListView Spyru Windows Forms 4 10-06-2003 07:55 PM
how can i check if my dataset is empty? yaniv Database / XML / Reporting 3 04-09-2003 01:39 AM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe