in reply to Recursive sub

A couple of remarks on top of those of BrowserUK:

1) Perl has an elsif option, so you can simplify you sub thusly:

sub recurseDir2 { my @temp = (glob $_[0]); foreach (@temp) { if (-d $_) { print "Directory: $_" , "\n" ; recurseDir2("$_/*"); } elsif (-f $_) { print "File: $_ \n"; } } }

2) There might (I'm not sure with Windoze) be some 'things' in your directories meet neither the -f nor the -d test. In which case, I would recommend adding an else clause:

sub recurseDir2 { my @temp = (glob $_[0]); foreach (@temp) { if (-d $_) { print "Directory: $_" , "\n" ; recurseDir2("$_/*"); } elsif (-f $_) { print "File: $_ \n"; } else { next; } } }

hth

dave

Replies are listed 'Best First'.
Re: Re: Recursive sub
by dragonchild (Archbishop) on Sep 23, 2003 at 21:07 UTC
    Your else clause is unnecessary, as it's not doing anything. There definitely will be stuff (in many directories) that don't meet the -f or -d tests. In that case, he will just fall through and the loop will continue with the next item.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      You're right, of course. I originally had something like this:

      my @anomalies; sub recurseDir2 { my @temp = (glob $_[0]); foreach (@temp) { if (-d $_) { print "Directory: $_" , "\n" ; recurseDir2("$_/*"); } elsif (-f $_) { print "File: $_ \n"; } else { push @anomalies, $_; } } } print "Anomalies: @anomalies";

      but as I found no 'anomalies' in my filepaths I stupidly changed my code to what I posted. Thanks for pointing out my mistake.

      dave

Re: Re: Recursive sub
by wolis (Scribe) on Sep 25, 2003 at 06:04 UTC
    Interestingly glob didnt work on my installation of ActivePerl.

    readdir() works a treat tho.. funny how the -d and -f does not always work and I see most files and folders listed in the else

    sub recurseDir2 { opendir(DIR, $_[0]); my @temp = readdir(DIR); closedir DIR; #my @temp = (glob $_[0]); foreach (@temp) { if (-d $_) { print "Directory: $_" , "\n" ; recurseDir2("$_/*"); } elsif (-f $_) { print "File: $_ \n"; }else{ print "Other: $_ \n"; } } } recurseDir2('c:/'); ---8<--- Other: deleteme.htm Other: I386 Other: DISCOVER Other: AUTOEXEC.BAT Other: CONFIG.SYS Other: deleteme.gif Other: BOOTLOG.TXT Other: WINDOWS Other: casa_gms.zip Other: Answer.txt Other: Recycled Other: sacoss_crm.zip Other: convert_rarossa.zip Other: mpcsetup.log File: console.zip Directory: data ---8<---
    because windows folders usually don't have extensions and files usually do have extensions this concept could be used in a controlled environment to recurse through all folders (things with no extension)
    ___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com