Delete Dircetories with contents in it

Hi,
 How to delete directories and sub directories when there are files in them in Perl
new_perl_userAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lokiaustinCommented:
The easiest way is to use backticks and just call the shell command that would do the same thing.

`rm -rf $dir`

as always there's a ton of other ways to do it, but this I think is it the most simple
rduke15Commented:
See
perldoc -q "How do I delete a directory tree"

Open in new window

:

Found in /usr/share/perl/5.10/pod/perlfaq5.pod
How do I delete a directory tree?
(contributed by brian d foy)

If you have an empty directory, you can use Perl's built-in "rmdir". If the directory is not empty (so, no files or subdirectories), you either have to empty it yourself (a lot of work) or use a module to help you.

The "File::Path" module, which comes with Perl, has a "rmtree" which can take care of all of the hard work for you:
               use File::Path qw(rmtree);
               rmtree( \@directories, 0, 0 );

Open in new window


The first argument to "rmtree" is either a string representing a directory path or an array reference. The second argument controls progress messages, and the third argument controls the handling of files you don't have permissions to delete. See the "File::Path" module for the details.
ropennerCommented:
in perl

system "rm -rf \"/tmp/testdir number 1\"";


will
remove (rm)
recursively (-r)... go through all subdirectories (for unix not windows)
and force (f) it to happen without prompting you if you want it to delete something
the directory and contents of directory "/tmp/testdir number 1"

The quotes are necessary if there are spaces in your file name... backslashes are to escape the quotes so they are not interpreted.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jodymanCommented:
rduke15's way would be better,  I've seen the havoc rm -r has caused in unix/linux systems, not pretty.  At least you have specific control using the module.

Jody
rduke15Commented:
And system("rm -rf ...") is Unix only. Perl's rmtree works on any system on which Perl does.

The original poster didn't specify it, but if it has to run on Windows, "rm -rf" will do no harm, but no good either.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.