http://qs1969.pair.com?node_id=279787

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.