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

Hi Monks. I need to loop through all files inside a folder (one by one), and just get all file names, to use the names somewhere else. So if the library contains: file1, file2, file3 then I would want to have this pseudocode: for all files in folder: { temp_name = get_current_file_name(); function(temp_name); } Thanks in advance!

Replies are listed 'Best First'.
Re: getting files names
by jettero (Monsignor) on Apr 27, 2007 at 13:12 UTC

    my @filez = <folder/*>;? Or is there more to this that I'm missing?

    On the other hand... from your "pseudo-code," you might want an iterator perhaps?

    sub get_filez { my @filez = <folder/*>; return sub { return shift @filez; } }

    -Paul

Re: getting files names
by derby (Abbot) on Apr 27, 2007 at 13:15 UTC
Re: getting files names
by swampyankee (Parson) on Apr 27, 2007 at 17:13 UTC

    Use glob or opendir and readdir to get a list of filenames, then loop through the list.

    For example,

    my $dir = '.' my @list = glob($dir . '/*'); # <- amended per merlyn's response
    or
    my $dir = '.'; opendir(my $dh, $dir) or die "Could not open $dir because $!\n; @list = readdir($dh); closedir($dh);
    will return a list of files in the current working directory. My preference is to use glob for this sort of thing. Usually, the number of files in a directory will be small (no more than a few thousand), so storing the names in an array should not be a problem. You may want to filter @list, so that it contains only the type of file you want, but this is easy using grep. Glob can process wildcards, so @list could be restricted by something like
    @list = glob($dir .'/A*');

    Alternatively, you could call readdir in a scalar context, and process the files one at a time. The code for this would look something like:
    my $dir = '.'; opendir(my $dh, $dir) or die "Could not open $dir because $!\n; while(readdir($dh)){ # do stuff to $_ } closedir($dh);

    As I said, since the number of files in a folder tends to be fairly small, I prefer to use glob, possibly in conjunction with grep to get a list of file names, and then loop through that list.


    Corrected call to glob; see merlyn's response.

    emc

    Insisting on perfect safety is for people who don't have the balls to live in the real world.

    —Mary Shafer, NASA Dryden Flight Research Center
      You said:
      my $dir = '.' my @list = glob($dir);
      But that's not going to work. Glob wants a glob pattern. As ".", there's not much of a pattern there. See some of the other answers in this thread.
Re: getting files names
by cengineer (Pilgrim) on Apr 27, 2007 at 14:17 UTC
    Hope this helps
    my $files = dirlist($someDirectory); foreach my $fileInDirectory (@$files) { dosomething($fileInDirectory); } sub dirlist { my $dir = shift; my @filelist; opendir(DIR, $dir) or die "can't opendir $dir: $!"; while (defined(my $file = readdir(DIR))) { # Skip "." and ".." next if ($file =~ /^\.\.?$/); push(@filelist, $file); } closedir(DIR); return \@filelist; }
Re: getting files names
by akho (Hermit) on Apr 27, 2007 at 21:26 UTC
    ummm…

    function($_) foreach (glob "$folder/*");
A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.