Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Search through directories

by Perl_User (Acolyte)
on Jul 31, 2003 at 21:01 UTC ( #279787=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to process files that are stored in /usr/home/*/logs/. when I try to open the logs directory with this code, it does not recognize the "*"
my $log_dir = "/usr/home/*/logs"; opendir (LOG, $log_dir) or die "Error: Unable to open $log_dir. $!\n";
This is not working. Can anyone help?! Thanks in advance

Replies are listed 'Best First'.
Re: Search through directories
by fglock (Vicar) on Jul 31, 2003 at 21:11 UTC
    my @log_dir = glob "/usr/home/*/logs"; for ( @log_dir ) { do_something if -d $_ }
      Thanks much. The glob function worked!
Re: Search through directories
by sauoq (Abbot) on Jul 31, 2003 at 21:16 UTC

    If it had worked, how was your single scalar going to hold them all? And how was opendir going to open them all at once? And how were they all going to be associated with a single directory handle? One way or the other, you'll need to loop over them. That said, read perldoc -f glob for an easy way to get that to expand. You'll want to use it something like this:

    for ( glob '/usr/home/*/logs' ) { # open the dir - handle errors # do stuff... }

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Search through directories
by Cine (Friar) on Jul 31, 2003 at 21:04 UTC
    Use File::Find, opendir can only open one directory at a time.

    T I M T O W T D I
Re: Search through directories
by Mr. Muskrat (Canon) on Jul 31, 2003 at 21:04 UTC

    Are you trying to open a directory that is called /usr/home/*/logs or all directories under /usr/home that contain a logs directory?

    Update: I've seen funky directory names before so that is why I asked.

      I am trying to open all the directories under /usr/home/ that contain a logs directory.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2022-10-03 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (14 votes). Check out past polls.

    Notices?