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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Deleting files

 



fishfork
Novice

Sep 6, 2000, 9:35 AM

Post #1 of 2 (269 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?

Thanks.


japhy
Enthusiast

Sep 5, 2000, 10:49 PM

Post #2 of 2 (269 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;
</pre><HR></BLOCKQUOTE>

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: $!";
</pre><HR></BLOCKQUOTE>


------------------
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