Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

File searching *

by michael99 (Acolyte)
on Mar 02, 2020 at 02:44 UTC ( [id://11113615]=perlquestion: print w/replies, xml ) Need Help??

michael99 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am doing file checks in a directory. Whenever there is any files other than $name.log and *.log, will be shown. $name is user specified. Currently everything is working, except *.log, it will still print other *.log (eg. a.log)

if (-e 'abc' && -d 'abc'){ opendir my $dh4, $a_directory or die "can't open directory:$!"; while (my $entry5 = readdir $dh4){ if ($entry5 ne $name.".log" && $entry5 ne '<*.log>'){ print OUTPUT ":E: $entry5 is extra file/dir \n"; print ":E: $entry5 is extra file/dir \n"; } } }

Replies are listed 'Best First'.
Re: File searching *
by davido (Cardinal) on Mar 02, 2020 at 03:33 UTC

    You could change your conditional as follows:

    if ($entry5 ne $name && $entry5 !~ m/\.log$/) { # ...

    You have this, currently: $entry5 ne '<*.log>', which doesn't really make sense. You clearly are not looking for a file named <*.log> but you are specifying exactly that within single quotes, which prevent the glob operator from doing anything useful. Additionally you don't really need the glob operator, they way you are testing the files.

    There's another puzzler in your code as well: What filename would match $name . '.log' but would not match *.log or m/\.log$/? What I'm getting at is that there doesn't seem to be any good reason to test explicitly for $name, since the part of the conditional to the right of the && operator would already catch everything that ends in .log.

    One thing also to bring up: You could probably avoid the readdir and while loop entirely by using the glob operator correctly:

    if (-e 'abc' && -d _) { foreach my $entry (<*.log>) { print OUTPUT ":E: $entry5 is extra file/dir \n"; } }

    This may or may not be exactly what you're after, but it's what your code seems to be trying to do.


    Dave

      Thanks Dave for the comment. !~ m/\.log$/ is working. Apparently you bring up a good point where $name.".log" is extra effort while the right condition has been included it.

      For the suggestion to avoid reaadir and while loop, since I cannot sure which file would exist to be searched, and I need to print out whose additional files which not in the spec defined. How would I defined the condition in 'foreach' ?

      if (-e 'abc' && -d _) { foreach my $entry (#other than <*.log>) { print OUTPUT ":E: $entry5 is extra file/dir \n"; } }
Re: File searching *
by BillKSmith (Monsignor) on Mar 02, 2020 at 05:05 UTC
    Perhaps this is what you mean.
    use strict; use warnings; my $name = 'user_supplied.txt'; my @errs = grep {$_ !~ /\.log$/ and $_ ne $name} glob(".* *"); print do{local $" = "\n"; "@errs\n"};
    Bill
Re: File searching *
by Marshall (Canon) on Mar 03, 2020 at 01:27 UTC
    You code is confusing. This if (-e 'abc' && -d 'abc'){} means that some subdirectory of the current directory exists called "abc". I don't see any connection between that and the code that follows. What does 'abc' have to do with anything? BTW, the -e test is unnecessary.

    To print the non-log files which are not directories within $dh4, something like this could work...
    When using readdir(), remember that the names that you get are simple names from that directory and that in order to do a file test on them you have to prepend the directory name.

    opendir my $dh4, $a_directory or die "can't open directory $dh4:$!"; while (my $this_file = readdir $dh4) { next if ($this_file =~ /\.log$/); #skip all log files next if (-d "$dh4/$this_file"); #skip all directories print "$this_file is an extra file: $dh4/$this_file\n"; }
    What is $entry5 and what does it have to do with anything?
    Perhaps you want extra log files that are not named entry5.log?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found