Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Get file names

by nofernandes (Beadle)
on Aug 26, 2003 at 17:07 UTC ( [id://286784]=perlquestion: print w/replies, xml ) Need Help??

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

Hello fellow monks,

I want to make a program that given one specific directory such as c:\something, it returns the full path of the files that are in that directory.

Example:

If the directory c:\something has this files: aa.a ab.a ac.a ad.a etc... the program should return C:\something\aa.a C:\something\ab.a .....

Is there any module that can do this? Or any function?

Thank you very much for your help

Nuno

Replies are listed 'Best First'.
Re: Get file names
by broquaint (Abbot) on Aug 26, 2003 at 17:18 UTC
    use IO::Dir; use File::Spec::Functions 'rel2abs'; my $d = IO::Dir->new("your_dir_here") or die("ack - $!"); my @files = map rel2abs($_), grep -f, $d->read;
    See. IO::Dir and File::Spec::Functions for more info.
    HTH

    _________
    broquaint

Re: Get file names
by tcf22 (Priest) on Aug 26, 2003 at 17:17 UTC
    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);

      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.
Re: Get file names
by Not_a_Number (Prior) on Aug 26, 2003 at 20:15 UTC

    I don't address efficiency here. Unless your directory is very big, your approach should be efficient enough.

    Just a couple of comments on your code, though:

    1) In windows, you don't need to use the backslash as a path separator thingy, you can use a forward slash, which doesn't need escaping. This means that instead of typing for example:

    my $path = 'c:\\perl\\progs\\testing';

    You can do:

    my $path = 'c:/perl/progs/testing';

    which is a lot easier to type (and easier to read).

    2) Change your line:

    close (DIR);

    to:

    closedir (DIR);

    3) Re the first line inside your sub:

    ($path)=@_;

    This is usually written something like:

    my $whatever = $_[0];

    or else:

    my $whatever = shift;

    Note that this uses a distinct variable limited to the scope of the sub, which is generally a Good Idea. Indeed, you could even do away with this line and have as the first line of your sub:

    opendir (DIR, shift) || die "$!";

    4) Lastly, considering this line:

    my @files=grep{/\.c$/} readdir(DIR);

    I would recommend that you also test that everything that you put into @files really is a file (rather than a directory, for example):

    my @files = grep{ -f and /\.c$/ } readdir(DIR);

    dave

Re: Get file names
by bm (Hermit) on Aug 27, 2003 at 11:12 UTC
    This one liner is recursive. I do not know if this is desired.

    perl -MFile::Find -e "find(\&w,shift);sub w{print \"$File::Find::name\ +n\"}" c:/temp

    --
    bm
Re: Get file names
by bm (Hermit) on Aug 27, 2003 at 11:12 UTC
    Duplicate of above. Please reap. Sorry.
    --
    bm

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://286784]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-18 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found