Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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 ( #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.

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

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? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2022-05-24 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?