[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: How to delete read-only files with Ruby commands

Kurt Euler

10/8/2003 6:21:00 AM

OK thanks!

-----Original Message-----
From: Hal Fulton [mailto:hal9000@hypermetrics.com]
Sent: Tuesday, October 07, 2003 11:17 PM
To: ruby-talk@ruby-lang.org
Subject: Re: How to delete read-only files with Ruby commands


Kurt Euler wrote:
> All-
>
> On a Windows NT system, is there a way to quickly delete directories (from within a Ruby script) that may have read-only files in them. The command I've been using is
>
> File.delete(*Dir["*.*"])
>
> but this crashes when it hits a RO file.
>
> Similarly, is there a way to force the overwriting of a RO file? I've been using File.syscopy, but this, too, crashes when attempting to overwrite an RO file.

I'd suggest this: Get a list of files first and iterate over it.
Wrap in a begin/end and catch the exception. When you get the
exception for a RO file, explicitly change it to be writable
and do a retry.

Something like:

files = Dir["*.*"]
files.each do |file|
begin
File.delete(file)
rescue Whatever
system("whatever #{file}")
retry
end
end

I don't know the exception or how to fix it in Windoze. Any other
mistakes I made are due to the lateness of the hour.

Hal