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

Hi I want to delete the contents of a directory and all it's subdirectories and files under it. Perl says you can add -U to include subdirectories but it's unsafe to your file system. Is it unstable or just dangerous to logic errors (if you do unlink(C:\\)? rmdir only removes the subdirectories if they are empty. I don't want to traverse a directory.. I rather shell out if that is necessary.

Replies are listed 'Best First'.
Re: unlink and -U
by Zaxo (Archbishop) on Jun 25, 2002 at 15:32 UTC
    use File::Path qw( rmtree );

    Please note the caveat about errors at the end its perldoc.

    Update: File::Path exports mktree and rmtree by default, so use File::Path; will suffice.

    After Compline,
    Zaxo

Re: unlink and -U
by bronto (Priest) on Jun 25, 2002 at 15:40 UTC

    Since you cite C:\\, I suppose you are on a Windows platform. If you prefer to shell out of perl you could try running a deltree from inside your script, but I think that you should really seriously consider Zaxo's File::Path hint.

    Ciao!
    --bronto

    --
    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Re: unlink and -U
by flounder99 (Friar) on Jun 25, 2002 at 15:47 UTC
    If you are using win9x you can use system "deltree /Y", $path; to delete a directory.
    On Win2k/WinNT you can use system "rmdir /S/Q", $path;

    --

    flounder

Re: unlink and -U
by robobunny (Friar) on Jun 25, 2002 at 15:33 UTC
    i think you want rm -fr instead of rmdir, but you had better have VERY good error checking if you do that :)