ok. File::Find. earlier today, i was looking rather smugly at Jendas node on File::Find; File::Find considered hard?, thinking how *i* would not be caught by the vagaries of the module, thinking even how i would start using merlyns File::Finder, once i had completed a simple task - removing directories via File::Find.

after all, there is a recipe for it in the perl cookbook. nothing could go wrong...

several fraught hours later, my confidence & hubris had dissolved in the acid of File::Find... to boot, my presumption led to the deletion of many funky files in my dat directory. after much wailing and gnashing of teeth, i appeal for advice, and hayulp...

the following script takes directories as arguments, and attempts, quite simply, to delete them after deleting all files inside them. actually, it's a little more generic than that - it is recursive directory deleter, and will delete any subdirectory inside the directories mentioned. it half works, be careful! i should mention, it's pretty much directly stolen from the cookbook.

# rmdirsimple.pl use File::Find qw(finddepth); use Cwd; *name = *File::Find::name; my @dirs = @ARGV; my @legitdirs = grep { -d } @dirs; finddepth (\&unlinkplus, @legitdirs); # danger, danger. high voltage. use at own risk. sub unlinkplus { warn "current dir :" . cwd . "\n"; if (!-l && -d _) { # not a link, is a directory warn "rmdir attempt :" . $name . "\n"; (rmdir($_) && warn "ok..." ) or warn "cannot rmdir $name:" . " $!"; } else { warn "file unlink :" . $name . ">"; (unlink($_) && warn "ok..." ) or warn "can't unlink " . $name . "$!"; } }
now, run this through cygwin perl (5.8.2), using something like
/usr/bin/perl rmdirsimple.pl a b
where a and b are directories that contain a.txt and b.txt respectively, and you will get a satisfying:
current dir :/home/k/kh/prlfrg/dat/a file unlink :a/a.txt> at rmdirsimple.pl line 20. ok... at rmdirsimple.pl line 21. current dir :/home/k/kh/prlfrg/dat/a rmdir attempt :a ok... at rmdirsimple.pl line 17. current dir :/home/k/kh/prlfrg/dat/b file unlink :b/b.txt> at rmdirsimple.pl line 20. ok... at rmdirsimple.pl line 21. current dir :/home/k/kh/prlfrg/dat/b rmdir attempt :b ok... at rmdirsimple.pl line 17.
run it through activestate (5.6.1) perl - and the deletion of the directories seems impossible: here's the results:
current dir :C:/cygwin/home/k/kh/prlfrg/dat/a file unlink :a/a.txt> at rmdirsimple.pl line 20. ok... at rmdirsimple.pl line 21. current dir :C:/cygwin/home/k/kh/prlfrg/dat/a rmdir attempt :a cannot rmdir a: Permission denied at rmdirsimple.pl line 17. current dir :C:/cygwin/home/k/kh/prlfrg/dat/b file unlink :b/b.txt> at rmdirsimple.pl line 20. ok... at rmdirsimple.pl line 21. current dir :C:/cygwin/home/k/kh/prlfrg/dat/b rmdir attempt :b cannot rmdir b: Permission denied at rmdirsimple.pl line 17.
most depressing. i am sure that it's because the process is sitting in the directory it is attempting to delete. but if this *is* the case, how did rmtree1 from the cookbook ever... get into the cookbook! and does anyone know how the above problem might be solved within the confines of activestate perl?

many thanks for any help
...wufnik

-- in the world of the mules there are no rules --

In reply to Yea, though i walk through the shadow of File::Find & Directory Tree Deletion... by wufnik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.