From: Kurt Euler Date: 2003-10-08T15:21:20+09:00 Subject: Re: How to delete read-only files with Ruby commands 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