in reply to Globbing for file in an unkown directory path only works first time in foreach loop?

Despite your updates, you still haven't shown us the code you are really running so I'm still not entirely sure what you are trying to accomplish but this should give you a push in the right direction.

#!/bin/env perl use strict; use warnings; my @day_of_year = (4257,4257,4258,4259); # If @day_of_year only contains the days then there is no reason for m +atching for my $day_of_year (@day_of_year) { print "looking for things in $day_of_year folder /home/myfiles/day_o +f_year/$day_of_year/*/desired_file.txt\n"; # there may be multiple directories containing desired_file.txt my @desired_paths = glob('/home/myfiles/day_of_year/'.$day_of_year.' +/*/desired_file.txt'); for my $info_desired_filepath (@desired_paths) { print "$info_desired_filepath \t\t is the file name \n\n"; <STDIN>; open (my $info_FH, '<', $info_desired_filepath) or die "cannot open $info_desired_filepath..........\n\n"; print "found a file!!!!!!\n"; chomp(my @lines = <$info_FH>); close($info_FH); print "lines:\n"; print "$_\n" for @lines; # yep, we actually read the data } } __DATA__ Directory structure: myfiles/ day_of_year/ |-- 4257 | |-- a | | `-- desired_file.txt | |-- b | | `-- desired_file.txt | `-- c | `-- desired_file.txt |-- 4258 | |-- a | | `-- desired_file.txt | `-- c | `-- desired_file.txt `-- 4259 `-- c `-- desired_file.txt Output: skimming for the day of year number: 4257 looking for things in 4257 folder /home/myfiles/day_of_year/4257/*/des +ired_file.txt /home/myfiles/day_of_year/4257/a/desired_file.txt is th +e file name found a file!!!!!! lines: data /home/myfiles/day_of_year/4257/b/desired_file.txt is th +e file name found a file!!!!!! lines: data /home/myfiles/day_of_year/4257/c/desired_file.txt is th +e file name found a file!!!!!! lines: data bogus data skimming for the day of year number: 4257 looking for things in 4257 folder /home/myfiles/day_of_year/4257/*/des +ired_file.txt /home/myfiles/day_of_year/4257/a/desired_file.txt is th +e file name found a file!!!!!! lines: data /home/myfiles/day_of_year/4257/b/desired_file.txt is th +e file name found a file!!!!!! lines: data /home/myfiles/day_of_year/4257/c/desired_file.txt is th +e file name found a file!!!!!! lines: data bogus data skimming for the day of year number: 4258 looking for things in 4258 folder /home/myfiles/day_of_year/4258/*/des +ired_file.txt /home/myfiles/day_of_year/4258/a/desired_file.txt is th +e file name found a file!!!!!! lines: data /home/myfiles/day_of_year/4258/c/desired_file.txt is th +e file name found a file!!!!!! lines: data bogus data skimming for the day of year number: 4259 looking for things in 4259 folder /home/myfiles/day_of_year/4259/*/des +ired_file.txt /home/myfiles/day_of_year/4259/c/desired_file.txt is th +e file name found a file!!!!!! lines: data

Updated: Added directory structure and output from my test run.

  • Comment on Re: Globbing for file in an unkown directory path only works first time in foreach loop?
  • Download Code

Replies are listed 'Best First'.
Re^2: Globbing for file in an unkown directory path only works first time in foreach loop?
by MarsRover (Novice) on Jan 29, 2016 at 20:46 UTC

    Thank you Muskrat for your reply. I did infact include the actual code i was running, both in the reply to the post (Anonymous Monk on Jan 29, 2016 at 20:09 UTC) and in my edit to the OP. I've just had to change the directory paths/names to something else because of security/proprietary reasons.

    I'm going to try your implementation now but one thing that i'm concnerned about in your code is you have the comment: # there may be multiple directories containing desired_file.txt

    in the /home/myfiles/day_of_year/4257/ directory there are multiple folders. One of those folders (with unknown name) has a path that leads to the single file desired_file.txt.

Re^2: Globbing for file in an unkown directory path only works first time in foreach loop?
by MarsRover (Novice) on Jan 29, 2016 at 20:54 UTC

    the directory structure looks something like

    /home/myfiles/day_of_year/255/data/JPF_374362/desired_file.txt

    /home/myfiles/day_of_year/256/data/EJF_264827/desired_file.txt

    /home/myfiles/day_of_year/257/data/FFE_387392/desired_file.txt

    where after the data directory there is a day specific ID (three letters, underscore, and then a bunch of numbers, all are unpredictable for each day) and within there is the file named desired_file.txt