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

Hey everyone, here it the first viewing of my code. It works for being a recursive directory searcher. The only part that doesn't work is the unlink portion. I'm still working on that. But, have a look and let me know what you think. Thanks for the support.
#!/usr/bin/perl -w use Cwd; print "Enter start directory: $workdir"; chomp($workdir = <STDIN>); sub scandir { my ($workdir) = shift; #c:\mydocu~1\Netscape my ($startdir) = &cwd; #c:\mydocu~1\perl chdir($workdir) or die "Unable to change to $workdir:$!\n"; opendir(DIR, ".") or die "Unable to open $workdir:$!\n"; my @files = readdir(DIR) or die "Unable to read $workdir:$!\n"; closedir(DIR); foreach my $file (@files) { next if ($file eq "."); next if ($file eq ".."); if (-d $file) { &scandir($file); next; } if ($file eq '/\.log$/i || /\.\d+\w+\-\d+\wm$/i') { unlink($file) || warn "Unable to delete $file: $!\n"; } else{ print "Found File: $file\n"; } chdir($startdir) or die "Unable to change to $startdir: $!\n"; } } &scandir(".");
Thanks Again!!!! curtisb = Upcoming Perl Programmer "Start Slow and Small!!!!"

Replies are listed 'Best First'.
(jcwren) Re: First code test view
by jcwren (Prior) on Oct 11, 2000 at 01:08 UTC
    Rather than writing your own directory scanner, you should consider using File::Find. Your code is going to have potential problems if you hit a symlink.

    There are a number of nodes regarding this problem, and it's one of merlyn's pet peeves. Take a look through them (you can find them from his homenode, or use 'super search' in the search box at the top to find) to see what the issues regarding looping through a directory yourself are.

    Here's a list for you (Gah! Doesn't ANYONE else besides merlyn talk about the danger of this? <G>) Re: Recursion problem, RE: RE: RE: RE: Re: Its not supposed to!, and WARNING t0mas wrote BAD CODE

    --Chris

    e-mail jcwren
      Doesn't ANYONE else besides merlyn talk about the danger of this?

      But merlyn does such a good job.. for me to attempt to do so is just setting myself up for disappointment, because merlyn always does such a nice job.. my response looks weak and pathetic in comparison..

      Besides, didn't you just do this?

Re: First code test view
by Trimbach (Curate) on Oct 11, 2000 at 01:24 UTC
    File::Find is a better solution for the directory-scanning part of your code, but I think your problem with unlinking is with this regex:
    if ($file eq '/\.log$/i || /\.\d+\w+\-\d+\wm$/i')
    First, you can't use "eq" when you want to use a regular expression. "eq" is a string comparison operator, so what you're asking Perl to do is see if $find ever equals the (uninterpolated) string '/\.log$/i || /\.\d+\w+\-\d+\wm$/i'. Unless you're running a REALLY strange file system you don't have any files named that. ;-D

    What you probably want is to change it over to a regular expression like this:

    if ($file =~ m/\.log$/i || $file =~ m/\.\d+\w+\-\d+\wm$/i)
    The =~ m operator is a matching operator which means your "if" statement is now looking for a $file that either ends in ".log" or ends in some sort of (looks like) date format. This is probably closer to what you want.

    Gary Blackburn
    Trained Killer

Re: First code test view
by curtisb (Monk) on Oct 11, 2000 at 07:39 UTC
    Hey, thanks for the support. I will take your advice and look at the File::Find module. Thanks again. curtisb