Getting all directories/files from current directory and using -d flag for the directories

Discussion in 'Perl Misc' started by Adam Petrie, Oct 8, 2004.

  1. Adam Petrie

    Adam Petrie Guest

    Hi guys/gals,

    I'm pretty new to Perl, so please keep that in mind. :)

    It looks like File::Find goes through all the subdirectories of the
    current directory, which isn't what I want. I just want all the
    contents of the current directory.

    On doing some research, many people have recommended doing something
    like the following:
    -----------
    $home_dir = ''; #changed to protect the innocent :)

    opendir(DIR,$home_dir) or die "Can't open the current directory:
    $home_dir $!\n";

    # read file/directory names in that directory into @names
    @names = readdir(DIR) or die "Unable to read current dir:$!\n";

    closedir(DIR);

    foreach $name (@names) {
    next if ($name eq "."); # skip the current directory entry
    next if ($name eq ".."); # skip the parent directory entry

    if (-d $name){ # is this a directory?
    print "found a directory: $name<br />";
    }
    else {
    print "found a file: $name <br />";
    }
    }
    -----------
    However, the -d doesn't seem to be working right. Most of the output
    comes from the else part when it should be showing many directories
    instead.

    What is going on? Any other solutions?
     
    Adam Petrie, Oct 8, 2004
    #1
    1. Advertising

  2. Adam Petrie

    Umesh Nair Guest

    Re: Getting all directories/files from current directory and using-d flag for the directories

    Adam Petrie wrote:
    > Hi guys/gals,
    >
    > I'm pretty new to Perl, so please keep that in mind. :)
    >
    > It looks like File::Find goes through all the subdirectories of the
    > current directory, which isn't what I want. I just want all the
    > contents of the current directory.
    >


    You might want to use the very convenient File::Finder module from CPAN.

    use File::Finder;
    my @array = File::Finder->type('f')->in($dir_loc);
     
    Umesh Nair, Oct 8, 2004
    #2
    1. Advertising

  3. Adam Petrie <> wrote:

    > $home_dir = ''; #changed to protect the innocent :)
    >
    > opendir(DIR,$home_dir) or die "Can't open the current directory:
    > $home_dir $!\n";
    >
    > # read file/directory names in that directory into @names
    > @names = readdir(DIR) or die "Unable to read current dir:$!\n";
    >
    > closedir(DIR);
    >
    > foreach $name (@names) {
    > next if ($name eq "."); # skip the current directory entry
    > next if ($name eq ".."); # skip the parent directory entry
    >
    > if (-d $name){ # is this a directory?
    > print "found a directory: $name<br />";
    > }
    > else {
    > print "found a file: $name <br />";
    > }
    >}
    > -----------
    > However, the -d doesn't seem to be working right. Most of the
    > output comes from the else part when it should be showing many
    > directories instead.
    >
    > What is going on? Any other solutions?


    Maybe your program's default directory while executing is not what
    you think it is? The chdir function could be useful in that case.
     
    David K. Wall, Oct 8, 2004
    #3
  4. Adam Petrie

    Jay Tilton Guest

    (Adam Petrie) wrote:

    : $home_dir = ''; #changed to protect the innocent :)
    :
    : opendir(DIR,$home_dir) or die "Can't open the current directory:
    : $home_dir $!\n";
    :
    : # read file/directory names in that directory into @names
    : @names = readdir(DIR) or die "Unable to read current dir:$!\n";
    :
    : closedir(DIR);
    :
    : foreach $name (@names) {
    : next if ($name eq "."); # skip the current directory entry
    : next if ($name eq ".."); # skip the parent directory entry
    :
    : if (-d $name){ # is this a directory?

    Better question: Does $name contain the path to the directory that was
    opened and readdir()ed earlier?

    Answer: Nope.

    use File::Spec::Functions qw(catfile);
    if (-d catfile($home_dir, $name) ) {

    : print "found a directory: $name<br />";
    : }
    : else {
    : print "found a file: $name <br />";
    : }
    : }
     
    Jay Tilton, Oct 8, 2004
    #4
  5. Adam Petrie <> wrote:


    > I'm pretty new to Perl, so please keep that in mind. :)



    The docs that come with perl are a great resource.

    You should become accustomed to looking at them as a *first*
    step in troubleshooting.


    > opendir(DIR,$home_dir) or die "Can't open the current directory:
    > $home_dir $!\n";
    >
    > # read file/directory names in that directory into @names
    > @names = readdir(DIR) or die "Unable to read current dir:$!\n";



    perldoc -f readdir

    anticipates the very problem you are having.

    Your problem could have been solved in about 20 seconds by
    using the docs...


    > if (-d $name){ # is this a directory?


    If you're planning to filetest the return values out of a
    "readdir", you'd better prepend the directory in question.
    Otherwise, because we didn't "chdir" there, it would have been
    testing the wrong file.


    > What is going on?



    You are using a function without having read its documentation.

    This is the programming equivalent of signing a contract without
    reading it first. Very dangerous!


    > Any other solutions?



    Read the documentation for the functions that you use.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 9, 2004
    #5
  6. Adam Petrie

    Tintin Guest

    "Adam Petrie" <> wrote in message
    news:...
    > Hi guys/gals,
    >
    > I'm pretty new to Perl, so please keep that in mind. :)
    >
    > It looks like File::Find goes through all the subdirectories of the
    > current directory, which isn't what I want. I just want all the
    > contents of the current directory.


    in that case, just do

    #!/usr/bin/perl
    use strict;

    foreach (</path/to/dir/*>) {
    print "Directory: $_\n" if -d;
    print "File: $_\n" if -f;
    }
     
    Tintin, Oct 9, 2004
    #6
  7. (Adam Petrie) wrote in
    news::

    > However, the -d doesn't seem to be working right. Most of the output
    > comes from the else part when it should be showing many directories
    > instead.
    >
    > What is going on? Any other solutions?


    It is always a good idea to actually read the documentation for the
    functions you are using:

    perldoc -f readdir

    Sinan.
     
    A. Sinan Unur, Oct 9, 2004
    #7
  8. On 8 Oct 2004 13:05:10 -0700, (Adam Petrie)
    wrote:

    >opendir(DIR,$home_dir) or die "Can't open the current directory:

    [snip]
    > if (-d $name){ # is this a directory?



    How strange! This very same question was asked here a few days ago,
    see:

    <41648726$0$1274$>

    and followups.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Oct 9, 2004
    #8
  9. Adam Petrie

    Adam Petrie Guest

    Tim Hammerquist <> wrote in message news:<>...
    > Assuming the directory structure:
    >
    > /data <- directory where you're looking for file/dirs
    > /bin <- directory where your script is located
    > /other <- another directory where your script is called *from*
    >
    > and $home_dir is set to "/data", then the directories and files returned
    > by readdir() will be in /data. However, when you do the -d test, the
    > test is performed relative to the directory from which the script is
    > executed ("/other") and so is checking whether /other/somefile is
    > a directory, rather than /data/somefile, as you intend.
    >
    > Just prefix $user_dir onto $name and test that way:
    >
    > : foreach $name (@names) {
    > : next if ($name eq "."); # skip the current directory entry
    > : next if ($name eq ".."); # skip the parent directory entry
    > :
    > : $name = "$home_dir/$name";
    > : if (-d $name){ # is this a directory?
    > : print "found a directory: $name<br />";
    > : }
    > : else {
    > : print "found a file: $name <br />";
    > : }
    > : }
    >
    > Oh, and remember to use strict;
    >
    > HTH,
    > Tim Hammerquist


    Thanks Tim,

    I wasn't adding the prefix for the full path. It works now!
     
    Adam Petrie, Oct 11, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    1
    Views:
    517
    Eliyahu Goldin
    Jun 23, 2004
  2. ptn
    Replies:
    3
    Views:
    288
  3. albert kao
    Replies:
    3
    Views:
    614
    Tim Chase
    May 14, 2010
  4. Dirk Einecke
    Replies:
    8
    Views:
    183
    Robert Klemme
    Apr 19, 2004
  5. Marc Heiler
    Replies:
    4
    Views:
    107
    ara.t.howard
    Nov 18, 2007
Loading...

Share This Page