Solved

"Deltree" command in PERL?

Posted on 1998-05-22
6
876 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

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

Accepted Solution

by:
bertvermeerbergen earned 50 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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