in reply to Get file names

openddir, readdir, and closedir are probably what you need.
my $dir = 'c:'; opendir(DIR, "$dir\\"); my @files; while(my $file = readdir(DIR)){ push(@files, "$dir\\$file") unless(-d "$dir\\$file"); } closedir(DIR); print "$_\n" foreach(@files);

Replies are listed 'Best First'.
Re: Re: Get file names
by nofernandes (Beadle) on Aug 26, 2003 at 17:42 UTC

    Yes.. i´ve already use that..!! Is this the best way or the most efficent way? Or is preferably to use modules instead of perl built in commands?

    I´ve made this code:

    use strict; use warnings; my $path="c:\\teste"; sub procura_ficheiros{ ($path)=@_; opendir(DIR, $path) ||die "$!"; my @files=grep{/\.c$/} readdir(DIR); close (DIR); return @files; } my @res=procura_ficheiros($path); foreach my $fich (@res){ my $final=$path."\\".$fich; print "Ficheiro: $path\\$fich\n"; print "Final: $final\n"; }

    It works.. but is it the most efficcient??

    Thank you once again

    Nuno

      You might want to have a look at the 'glob' function. It allows a wildcard expression and puts the results in an array.
      eg. (yes, it's cygwin, so YMMV)
      #!/bin/perl use strict; use warnings; foreach (glob ( "/cygdrive/c/temp/*" )) { print $_, "\n"; }

      Not amazingly _efficient_ (or so I'd imagine, I haven't profiled glob) but pretty straightforward code.
      Try using Benchmark to test the efficiency. I tried it, but I couldn't get broquaint's code to work. I was getting and empty directory listing. It may be a problem over here, haven't really looked into it yet.