Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

"Deltree" command in PERL?

Posted on 1998-05-22
6
Medium Priority
?
929 Views
Last Modified: 2008-02-01
Hi,
     I was wondering if there is a command to delete an entire directory and all the files and subdirectories in it.  Like the deltree command in dos.

Thanks in Advance.
0
Comment
Question by:cide
  • 3
  • 2
6 Comments
 

Expert Comment

by:GreenK
ID: 1206304
I think the easiest way is to call the system function 'rm -r'

That should remove things recursively which includes files and subdirectories...

So it would be something like this;

system('rm -r',<location>);

where location is the directory structure you want to delete....
0
 

Author Comment

by:cide
ID: 1206305
I don't think my ISP allows this.  I tried it and it went trough fine with no errors,  but the directory is still there.  Is there another way?
0
 
LVL 2

Expert Comment

by:bertvermeerbergen
ID: 1206306
You could use the 'File::Find' module.  The finddepth() will traverse a directory and all its subdirectories (depth-first order), calling a subroutine you define for every directory and file entry in the tree.  Your subroutine should use the '-d' operator on the entry:  use rmdir() if the entry is a directory, use 'unlink()' for a file.
If you are familiar with the UNIX find command, this module is the Perl implementation.  It is a very flexible module to do anything you like with directory entries, not only to delete them...
Remark that you need access rights to delete files and directories.  If 'rm' doesn't work because of security, you may not be able to do it any other way.  On the other hand, there is no specific 'delete' access in UNIX (it is implicit if you have write access on the directory), so if you can create files you should be able to delete them.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cide
ID: 1206307
What is the syntax for finddepth()?
0
 
LVL 2

Accepted Solution

by:
bertvermeerbergen earned 150 total points
ID: 1206308
Here is an example.  I did not include error checking code in the subroutine.  You should add this checks in a real program.

use File::Find

#-- The subroutine that will handle the deletion

sub DelTreeItem
{
  #-- $File::Find::dir contains the current directory (chdir() has been done)
  #-- $_ contains the current file or subdir name
  #-- $File::Find::name is "$File::Find::dir/$_"

  if (-d)  # implicit test with $_
  {
    rmdir;  # implicit use of $_, returns 1 if ok, 0 otherwise
  }
  else
  {
    unlink;  # implict use of $_, returns number of file removed
  }
}

#-- (this is in your main or some other subroutine)
#-- Parameters:
#--   1: A reference to the subroutine to be called for every item
#--   2: The top-level directory you want to remove
$DirPath = "/x/y/z";
finddepth(\&DelTreeItem, $DirPath);


0
 

Author Comment

by:cide
ID: 1206309
Ok, I fixed mine up the only problem is it's REALLY slow, is it because I'm using Find?  Is there anyway to fix this?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question