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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

empty file

Status
Not open for further replies.

Kasd2002

Programmer
Dec 10, 2002
4
DE
Hi there!
Can anyone tell me how can i check if a file is empty (and the file of that size if it is not empty)?
Thanks
 
oh ya, and for not empty,

if (-z $filename) {
print "file is empty\n";
}else {
print file size is %d\n", -s $filename;
}


That might work too....

-pd
 
Code:
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
       $atime,$mtime,$ctime,$blksize,$blocks)
           = stat($filename);

#$size is what you'll be looking for

$size ? "print size is $size" : print "empty";

---
cheers!
san
pipe.gif


"The universe has been expanding, and Perl's kind of been expanding along with the universe" - Larry Wall
 
The -s operator return the file size of the file and a zero filesize will evaluate to false. so a slight modification of ProbablyDown's script would give you
Code:
if ( $size = -s $filename ) {

    print "file '$filename' has size $size\n";

} else {

    print "file '$filename' has zero size\n";
}
which only involves one system call.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top