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 (again)

 



fishfork
Novice

Sep 10, 2000, 7:31 AM

Post #1 of 4 (318 views)
deleting files (again) Can't Post

This doesn't do anything and I can't work out why. Any ideas?

opendir(DIR,"$removePath") or error("Could not open project directory to delete files");
while( defined( $file = readdir(DIR) ) ) {
unlink( "$removePath/$file")
or error("Could not delete $file. Some files were not deleted properly: please contact the site administrator.");
}
closedir(DIR);


Thanks.


perlkid
stranger

Sep 10, 2000, 11:28 AM

Post #2 of 4 (318 views)
Re: deleting files (again) [In reply to] Can't Post

 
If your trying to delete all the files in a directory try this.

unlink("$removePath/*");

perlkid


japhy
Enthusiast

Sep 11, 2000, 10:36 PM

Post #3 of 4 (318 views)
Re: deleting files (again) [In reply to] Can't Post

First, NO, unlink() does not understand file globs. So you can't take that approach.

The problem with your code is that you don't check to see if $file is a file or directory. As it turns out, the first two values returned by readdir() are '.' and '..'. You CAN'T use unlink() to remove "foo/.". So your program gives an error.

Try:

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


unlink "$dir/$file" if -f "$dir/$file";
</pre><HR></BLOCKQUOTE>

Add whatever error-checking you'd like.

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



perlkid
stranger

Sep 12, 2000, 11:15 AM

Post #4 of 4 (318 views)
Re: deleting files (again) [In reply to] Can't Post

 
Cool Japhy,

I like That.

Thanks For The Correction,

perlkid

 
 


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

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