Solved

"Deltree" command in PERL?

Posted on 1998-05-22
6
901 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
File Find regex problem 4 70
Matching a random pattern with one common character 2 100
read an xml file in perl 2 86
Perl script to process a .csv file 18 86
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

752 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