in reply to Re: Still problems with recursive coding.
in thread Still problems with recursive coding.

Someone with little or no experience with Perl is going to find it impossible to maintain your code as you have it above. Even we are having a hard time with it.
use File::Find; use strict; my $start_from = 'c:\mydocu~1'; if (shift(@ARGV) =~ /all/) { find(\&search_all, $start_from); } else { find(\&search_none, $start_from); } sub search_all { unlink if /\.\d+\w+\-\d+\wm$|\.log$/i; } sub search_none { unlink if /\.\d+\w+\-\d+\wm$/i; }
Note that I'm not having it print "No files to delete". If you want that feature you'll have to set a flag or something when you delete something, and check for that flag when your script exits. As you're doing it in your code, you'll get a "No files to delete" message for every file that does not match your regexp criteria, which is probably not what you want.

Replies are listed 'Best First'.
RE: RE: Re: Still problems with recursive coding.
by curtisb (Monk) on Nov 14, 2000 at 02:34 UTC
    That is a much easier way of doing it. One question, how do you set the flags you are talking about?
    I also took a look at the else statements I wrote and you are correct. Why do I want to print out the statement once for everyfile that does not match? So I corrected protion of the code. Once again you are the man, thanks.
    curtisb
      For brevity's sake:
      my $found_any = 0; ... sub search_all { unlink, $found_any++ if /.../; } # (repeat for search_none) print "No files found!\n" unless $found_any;
      Make sense? You basically set a flag (a variable) if you find anything to unlink, and when your script is completed, check to see if this variable has a value, and if it doesn't, that means nothing matched, and you can alert the user to this fact. You can go the other way as well:
      if ($found_any) { print "$found_any files deleted.\n"; } else { print "No files were found to delete.\n"; }
        Thanks, Fastolfe! I'm still new at this. Trying to learn as fast as I can. Thanks. Curtisb