CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Deleting files



Sep 6, 2000, 9:35 AM

Post #1 of 2 (490 views)
Deleting files Can't Post

As part of a database system, deleting a record involved deleting a folder and all the files in it. I don't need to check what the files are, just delete the whole thing.

Whats the best way to delete a directory and all the files in it?



Sep 5, 2000, 10:49 PM

Post #2 of 2 (490 views)
Re: Deleting files [In reply to] Can't Post

If the directory has OTHER directories in it, then you need to use a recursive approach, like the standard File::Path module:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

use File::Path 'rmtree';
rmtree $path;

If it's just a directory with files, you can delete the files and then the directory:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

opendir DIR, $path or die "can't open $path: $!";
while (defined($file = readdir DIR)) {
unlink "$path/$file" or warn "can't delete $path/$file: $!";
closedir DIR;
rmdir $path or warn "can't remove $path: $!";

Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives