Solved

"Deltree" command in PERL?

Posted on 1998-05-22
6
854 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:cide
Comment Utility
What is the syntax for finddepth()?
0
 
LVL 2

Accepted Solution

by:
bertvermeerbergen earned 50 total points
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now