Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How do I test whether a file has some contents or not?

by Hen_true (Initiate)
on Oct 15, 2000 at 11:15 UTC ( [id://36796]=perlquestion: print w/replies, xml ) Need Help??

Hen_true has asked for the wisdom of the Perl Monks concerning the following question: (files)

I'm trying to test whether a file has some text in it or not. How do I do that ?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I test whether a file has some contents or not?
by ybiC (Prior) on Oct 15, 2000 at 17:13 UTC
    Filetest operators can be found at perlfunc:_X.   Just a day or so ago swiftone showed me the "_" shortcut for performing multiple tests on one file.   Yet another way to look at it.
    if (-s $file && -T _) { # do stuff with text file > 0bytes } if (-s $file && -B _) { # do stuff with binary file > 0bytes }
Re: How do I test whether a file has some contents or not?
by merlyn (Sage) on Oct 15, 2000 at 15:09 UTC
    Hmm, on the off chance that "contents" is not "text", and believing the description more than the title, I'd also add:
    print "it's a non-empty text-ish file" if -s $file and -T $file;
Re: How do I test whether a file has some contents or not?
by Anonymous Monk on Oct 15, 2000 at 12:28 UTC
    if ( -s 'file') { print "file exists and has size > 0\n"; }

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://36796]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found