Hi ytjPerl

Some of the modules you might want to use for your purpose (including File::Path already mentioned by LanX) are core modules (i.e. most probably already installed on your platform). Use them insofar as possible.

Assuming you really can't use these modules and that you want pure Perl, you could try this recursive subroutine:

sub deldir { my $dir = shift; my @entries = glob "$dir/*"; for my $entry (@entries) { unlink $entry if -f $entry; deldir($entry) if -d $entry; } rmdir $dir; } my $dir_to_be_deleted = 'C:\Users\tyj\Documents\Traning\2017'; deldir $dir_to_be_deleted;
CAVEAT:

1. I haven't tested it;

2. You might have to change some things (such as "$dir/*" to "$dir\\*") under Windows (probably not needed, though);

3. Make sure you fully understand how it works before trying to use it: this subroutine is designed to wipe out a full directory tree, and it could possibly delete your entire filesystem if wrongly used;

4. Last, BUT NOT LEAST, YOU SHOULD THOROUGHLY TEST it on dummy directories before you proceed.

In view of the above warnings, please read carefully the following notice:

This subroutine is provided "as is", without any warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with this subroutine or the use or other dealings in this subroutine.


In reply to Re: delete folder and its content with plain perl by Laurent_R
in thread delete folder and its content with plain perl by ytjPerl

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.