I am looking a small script which will tell me whether a file is an empty file or not, but I am unable to display this.
I have used the below code:
opendir DIR,$directory ;
while (my $dir =readdir DIR) {
if (-s "$dir") {
print "This is an empty file";
}
}
Here I am unable to print "This is an empty file" and my code does not went inside the if loop. Can anyone tell me what is the wrong in the above code?
The relevant file test operators are:
You are checking if $dir
is non-empty, so opposite of what you are trying to achieve. Use -z
(or !-s
) instead.
Also, each $dir
is just the filename without the path, so you need to include it yourself if you aren't processing the current directory.
if (-z "$directory/$dir") {
print "This is an empty file";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With