in reply to delete folder and its content with plain perl
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:
CAVEAT: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;
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.
|
|---|