Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statement to check if file is empty?

Status
Not open for further replies.

PhoenixDown

Programmer
Jul 2, 2001
279
CA
will this work?

if (/dir/file.cgi} !~ /[a-zA-Z0-9]/) {
$TotalEntries = "0";
} - PLEASE GO THERE! WE NEED MORE MEMBERS! :)

-------------------

Sometimes when I go to the main page of tek-tips.com, I get:

HTTP Error 405
405 Method Not Allowed

The method specified in the Request Line is not allowed for the resource identified by the request. Please ensure that you have the proper
 
I doubt it. What are you trying to accomplish?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I want it to check if it has 0 entries. Because currently it just says "Guestbook 1 - Number". - PLEASE GO THERE! WE NEED MORE MEMBERS! :)

-------------------

Sometimes when I go to the main page of tek-tips.com, I get:

HTTP Error 405
405 Method Not Allowed

The method specified in the Request Line is not allowed for the resource identified by the request. Please ensure that you have the proper
 
How about this:
Code:
if ( -z "$filename" ) {
   print "File has zero size\n";
}
the -z returns true if the file has zero size.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
To see if a file is empty, use the '-s' file test operator.

For example:
unless (-s '/dir/file.cgi') {
$TotalEntries = 0; # File is empty or does not exist
}
 
These both work and both have advantages depending on what you are doing. If you just want to test to see if the file exists, the -z is best. If you believe the file could exist and want to know how many bytes the file contains (which could be nothing but whitespace or a mixture of whitespace and other bytable information that takes up size), then the -s is good to use.

I would say that there is no reason to use the -s if you just want to test whether the file exists or not. The -s, I believe, takes 2-3 calls whereas the -z just 1. ICQ: 54380631
 
If you want to check whether the file exists, you would use -e, wouldn't you? -z returns true if the file exists but is zero-size.

I could be wrong, though. But I always use -e to see if the file exists.
 
True, -e returns true if the file exists. -z returns true if the file exists and has zero size. -s returns true (and the size) if the file exists and has non-zero size. They all have their uses. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Yes Grahf,

-e is one of the trio test operators available.

The -e does a TRUE 'file exist' seek and that is all it does. The problem though, is that what if the file exists, but has zero size (and I mean absolutely 0), you may or may not want to determine if it in fact has a size greater than 0.
ICQ: 54380631
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top