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

Ok, lets try this again. Sorry about the formating of the last message.

I am new at perl and am trying to get a handle on how the basics work.

All I am trying to do is search a directory in windows and return wether the items in it are files or directories.

What I have so far is this...
if I have the prompt at C: and I run the lines of code below only a couple of the directories are found. If I then change to D: in the prompt and run it again I get what I expect. For what reason does it do this. I have seen code that works but for my own understanding I would like to know what is going wrong here.

opendir dirs2, "D:/" or die "Could not open directory: $!"; while ($_ = (readdir dirs2)) { if (-d $_) { print "Directory: $_\n"; } }
Thanks
Paul Neale

Replies are listed 'Best First'.
Re: Searching directories
by LazerRed (Pilgrim) on Sep 23, 2003 at 01:48 UTC
    I do not have a D: on this win box, but maybe modification will help you see what is going on. I run my version from C:/ and get the desired results.

    my $dirs2 = "C:/WINNT"; opendir $dirs2, "C:/WINNT" or die "Could not open directory: $!"; while ($_ = (readdir $dirs2)) { if (-d "$dirs2/".$_) { print "Directory: $_\n"; } }

    What's happining with your script is:

    You are reading the contents of D: while running the script from C:
    Your list of files are created, then you want to check if they are directories. Your directory check is being performed with a C: path from the files read in from D: because "readdir" does not include the absolute pathname of a file. So, unless you have a directory on D: that is the exact same as what is on C: nothing will be printed.

    Hope this helps.

    LR

    Update: For clarity, you need to provide a fully qualified path to your directory check, because perl will default to the current directory it was run from if you do not. (or use a cwd)

    Whip me, Beat me, Make me use Y-ModemG.
      This is allot of help LazerRed:)
      I was wondering if this could be what it was, and was looking at ways of biulding the path but just couldn't get it right at all. Still allot to learn at this point.

      As for the other two suggestions thanks but I think they are a little ahead of me at this point. I will keep them in mind and come back to it at some point.

      I now have what I want but instead of using openDir I am now using glob. Is this a good way to go for a recursive sub?

      Thanks again
      Paul Neale

        Is this a good way to go for a recursive sub?
        Generally accepted answer: no. Use File::Find, which is a standard module, or maybe File::Find::Rule, which many people seem to prefer — I don't know it personally, but I thought I'd mention it anyway, otherwise there is bound to be a follow-up to this post pointing to it. :)
Re: Searching directories
by Roger (Parson) on Sep 23, 2003 at 05:53 UTC
    One advise, use the IO::Dir module from CPAN, that's the future of Perl.
    use IO::Dir; $root = "D:\"; $dir = new IO::Dir $root or die "Can not list directory"; while( $_ = $dir->read ) { print "Directory: $root\\$_\n" if (-d "$root\\$_"); }
      How about this way, tested on windoze 2000/Xp
      use strict; use File::Find; use Win32::File; use Cwd; my $dir = cwd; my $attr; my $output ="dump.txt"; open(OUTPUT,">$output") || die "can't open $output: $!"; &File::Find::find(\&wanted,"$dir"); sub wanted{ print OUTPUT "Directory $_ \n" if(-d $_); -f $_ && (Win32::File::GetAttributes($_, $attr)) && print OUTPUT "File +\t\ $_\n"; } close(OUTPUT);
        Ah... but this approach took away the portability of the code with the use of the Win32 module. Unless of course you only want the script to run on a Windows box.
Re: Searching directories
by PriNet (Monk) on Sep 23, 2003 at 03:32 UTC
    heres my 2 bits worth.... hope it helps...
    opendir (BUFFER, $DirectoryName); @Recieved = readdir(BUFFER); closedir (BUFFER); $Dummy = shift(@Recieved); $Dummy = shift(@Recieved); @Recieved = sort(@Recieved);


    you mean there's any easier way?