Link to home
Start Free TrialLog in
Avatar of cide
cide

asked on

"Deltree" command in PERL?

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.
Avatar of GreenK
GreenK

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....
Avatar of cide

ASKER

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?
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.
Avatar of cide

ASKER

What is the syntax for finddepth()?
ASKER CERTIFIED SOLUTION
Avatar of bertvermeerbergen
bertvermeerbergen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cide

ASKER

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?