Page 1 of 1 1
Topic Options
#188511 - 2008-07-02 04:31 PM detect empty text files
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
Hi everyone. Does anyone know of a UDF that will detect all empty text files in a given directory? By empty I mean absolutely no text inside the file.

Thanks.

Top
#188512 - 2008-07-02 04:35 PM Re: detect empty text files [Re: booey]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You don't need a UDF.

Enumerate the directory using DIR(), then use GetFileSize() on any files that you find.

If the file size is zero then the file is empty.

Top
#188513 - 2008-07-02 04:35 PM Re: detect empty text files [Re: booey]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
As a rule of thumb (in other words I can not gaurantee the accuracy of my claim), if you were to check the file size and it = 0 bytes then it would be empty.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#188515 - 2008-07-02 04:46 PM Re: detect empty text files [Re: Gargoyle]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Here is an example:

BREAK ON

$sDir="D:\TEMP"

$sFile=DIR($sDir)
While $sFile
If Not (16 & GetFileAttr($sDIR+"\"+$sFile))
If Not GetFileSize($sDir+"\"+$sFile)
"File "+$sDir+"\"+$sFile+" is empty"+@CRLF
EndIf
EndIf
$sFile=DIR()
Loop

Top
#188516 - 2008-07-02 04:59 PM Re: detect empty text files [Re: Gargoyle]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
Gargoyle, the "empty" files were showing up as 1KB even though there appeared to be nothing in them. However, upon further review, there is one blank line in the file that's causing it to be greater than 0 bytes. If I delete that line, then it goes to 0 bytes.

The files are created using redirectoutput. I'll have to look into why it's putting a blank line in there. I think I have an idea.

Thanks for the response.

Top
#188517 - 2008-07-02 05:00 PM Re: detect empty text files [Re: Richard H.]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I find that it is not usually as simple as the OP initially states and that once info is pulled through the nose, a UDF is the real true answer.

I like Bryce's DirPlus() UDF. It is very scalable and returns the COM handle allowing many FSO properties and methods to be used.

I would think after 61 posts, the OP would be familiar with our UDF library.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#188519 - 2008-07-02 05:31 PM Re: detect empty text files [Re: booey]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
If you cannot get rid off the blank line from RedirectOutput() then you have a couple of choices
  1. Read each file and check if there are only blank lines
  2. Determine if there is a cutoff size below which you can assume that a file is empty. For example, if a file with data is never smaller than 20 bytes then you can assume that a file with less than 20 bytes is empty.

Top
#188520 - 2008-07-02 05:37 PM Re: detect empty text files [Re: Richard H.]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
Thanks to all who responded.

I just used DirPlus and the file.size FSO to determine that a 3 byte file is empty with only a blank line.

Top
#188523 - 2008-07-02 06:57 PM Re: detect empty text files [Re: booey]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
*nix systems have an "od" utility that can display the contents of any file. By default, it does an octal dump, but -c will display the text, and -x will display hex. You can get a similar utility for Windows command lines from CygWin, or from the KMR Consulting web site.

Using such a utility will help you understand exactly what is inside a file. Once you understand the content, you can properly adjust the logic in your script.

Further, you'll find that an empty file with a "blank line" is actually two bytes - a carriage return and a line feed. If you don't trap the return from RedirectOutput, you'll get 3 bytes - the "0", a CR, and an LF.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1452 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.061 seconds in which 0.026 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org