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

Urgh! I've been working on this for too long. All I want is a filename array minus '.', '..', and any folders. This was my last attempt: @files = grep { !/^\./ && -f } map {$rootPath . $input->param('path') . "/" . $_ } readdir DIR;

Help! TIA

======================
Sean Shrum
http://www.shrum.net

Replies are listed 'Best First'.
Re: Need help with creating a filename only array
by BrowserUk (Patriarch) on Jul 27, 2002 at 07:01 UTC

    Your question is ambigious. Do you mean you want a list of all files and dirs minus . and ..?

    Or, all files, excluding all dirs (including . and ..)?

    Choose from the following code:

    #! perl -w local ($\=" | "); $dir = pop; opendir (DIR, $dir ) or die "Open '$dir' failed: $!"; my @filesOnly= map { $dir.$_ } grep{ -f "$dir/$_" } readdir(DIR); closedir (DIR) or die "Close '$dir' failed: $!"; print for (@filesOnly); print "-----------------------------------------------\n"; opendir (DIR, $dir ) or die "Open '$dir' failed: $!"; my @filesAndDirs= map { $dir.$_ } grep{ $_ ne "." and $_ ne ".." } rea +ddir(DIR); closedir (DIR) or die "Close '$dir' failed: $!"; print for (@filesAndDirs)
Re: Need help with creating a filename only array
by chromatic (Archbishop) on Jul 27, 2002 at 06:53 UTC

    '.' and '..' appear to be directories on my free Unix, so why not just do the -f test? I can't comment on the rest of your code, as I have no idea where the other variables come from.

Re: Need help with creating a filename only array
by flocto (Pilgrim) on Jul 27, 2002 at 07:02 UTC

    Your mistake was, that you prepended to filename and afterwards your regex wouldn't match anymore. Here's a fix:

    my @files = (); my $dir = $rootPath . $input->param('path'); $dir .= '/' unless $dir =~ m#/$#; opendir (DIR, $dir) or die $!; @files = grep { -f } map { "$dir$_" } grep { !m#^\.# } readdir (DIR); closedir (DIR);

    That's the working version of your code. But all hidden files (on *n?x, that is) are removed, too. If you want them to, all you have to do is remove that regex. "." and ".." will get filtered out as well since they are directories, too. At least on my box it worked all fine. That one line should then look like this:

    @files = grep { -f } map { "$dir$_" } readdir (DIR);

    Regards,
    -octo

      That almost works. However, I just need the filename (minus the drive and path):

      g:/this/that/other/somefile.txt needs to go into the array as somefile.txt

      Your code above, while it gets rid of folders (including '.', '..'), puts the drive, path, and filename into the array (like above).

      How do I get just the filename?

      TIA

      ======================
      Sean Shrum
      http://www.shrum.net

        Oh.. Well, I tried to stick as close to your code as possible. This should work for you though:

        @files = grep { -f "$dir$_" } readdir (DIR);

        Simple, eh? ;)

        Regards,
        -octo

Re: Need help with creating a filename only array
by George_Sherston (Vicar) on Jul 27, 2002 at 09:54 UTC
    File::Find will repay a little effort in learning to use it, as it will let you do pretty much whatever you want along these lines. If I understood your question, I think this does the particular thing you want at present:
    use File::Find; sub myprint {print "$_\n" unless -d($_)} # i.e. print anything that's not a directory find(\&myprint,'/your/directory');


    § George Sherston