Gossamer Forum
Home : General : Perl Programming :

How to delete a dir with all inside

Quote Reply
How to delete a dir with all inside
How in Perl, I delete a dir and all it contents, no matter if it has dirs or files inside, i want to del everything!
Quote Reply
Re: How to delete a dir with all inside In reply to
Define the directory as a variable:

Code:
$dir = "/ABSOLUTE/PATH/TO/DIR";

Change this to your absolute path to your directory.

Then add the following codes in your Perl script:

Code:
delete $dir;

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.
Quote Reply
Re: How to delete a dir with all inside In reply to
But if this dir has something inside, delete $dir will return an error, won´t it?
Quote Reply
Re: How to delete a dir with all inside In reply to
rmdir() will also return an error - "Deletes the directory specified by FILENAME if that directory is empty". ( http://www.perl.com/.../perlfunc/rmdir.html )

Why not just use system() or exec() to send the "rm -R" command?

Alternatively, use unlink() to remove the files, and then use rmdir() to remove the directory. Seems a waste of resources though, since you'll have to check for the filenames first.

Course, whatever you do, you'll need to ensure you have the permissions to do it.

adam
Quote Reply
Re: How to delete a dir with all inside In reply to
You could always look at how Wipeout works, and use that method. It will remove the contents of the directory, including all subdirectories under it.

http://www.cybernox.com/scripts/wipeout.html

--mark
Quote Reply
Re: How to delete a dir with all inside In reply to
look at http://www.perl.com/pub/doc/manual/html/pod/perlfunc/unlink.html
and also check the rmdir() link

------------------
LookHard Search
lookhard.hypermart.net
Lavon Russell


Quote Reply
Re: How to delete a dir with all inside In reply to
Wipeout came up with 500 error, and did not delete the directory tree. I did set the path correctly, and I did upload it in ascii, and I did chmod it to 755 -- so don't tell me about that Wink
Quote Reply
Re: How to delete a dir with all inside In reply to
check your perl path then. wipeout is like 5 lines of code, and there is nothing in it that would have a problem running as a cgi application.

--mark
Quote Reply
Re: How to delete a dir with all inside In reply to
Propably you dont have File::Path module installed!!
Quote Reply
Re: How to delete a dir with all inside In reply to
 
Quote:
Propably you dont have File::Path module installed!!

That's a good point. Could be, but that should be part of the standard perl installtion.

If you have telnet access, from the command line type:

perl -e 'use File::Path'

If nothing happens, then it is installed. If you receive an error, then there is your culprit. Smile

--mark
Quote Reply
Re: How to delete a dir with all inside In reply to
Oh, yeh! I just found it -- File::Path module is missing.
Unlucky me Frown

Is there any way to delete a dir w/ files without using this File::Path module?