Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Well I don't know your system but on Win32 its tricky. Here is how. Its actually a subroutine to check if a directory is empty and then remove it (of course you could just force it to remove it with the the "rmtree" command, but better to be safe).

Also uncomment the commented-out section if you want it to display the folder permissions mask before you alter them.

What I don't like about this code is that doing a directory listing to check if a directory is empty is slow, especially if a directory is large. There could be an easy command to do that that I'm simply not aware of (probable!).
#!perl use Win32::FileSecurity; use Cwd qw(cwd); use File::Path; use strict; use warnings; sub remove_dir { my ($path) = @_; my $currpath = cwd; my $ok = chdir $path; if ($ok) { my @dir = <*>; ## check if directory is empty if (!@dir) { my $dirmask = Win32::FileSecurity::MakeMask( qw(FULL GENER +IC_ALL) ); my %hash; # if ( Win32::FileSecurity::Get( $path, \%hash ) ) { # while( (my $name, my $mask) = each %hash ) { # print "$name:\n\t"; # my @happy; # Win32::FileSecurity::EnumerateRights( $mask, \@happy +) ; # print join( "\n\t", @happy ), "\n"; # } # } Win32::FileSecurity::Get( $path, \%hash); $hash{Administrator} = $dirmask; $ok = chdir $currpath; if ( Win32::FileSecurity::Set($path, \%hash) ) { print "\n\t$path --> Permissions Changed"; } else { print "\n\t$path --> Permission change failed$!\n$^E"; } if (rmtree $path) { print "\n\t$path --> Removed"; } else { print "\n\t$path --> Can't be removed $!\n$^E"; } } } }

The Funkster of Mirth
Programming these days takes more than a lone avenger with a compiler. - sam
RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers

In reply to Re: How can I Check the permissions of the folder,and correct them? by crabbdean
in thread rmtree deletes everything inside the folder but doesn't deletes the root folder him self by Nelly

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (8)
    As of 2018-06-22 10:34 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (124 votes). Check out past polls.