How to delete read-only files with Ruby commands

Interesting! Thanks!

···

-----Original Message-----
From: Gavin Sinclair [mailto:gsinclair@soyabean.com.au]
Sent: Tuesday, October 07, 2003 11:42 PM
To: ruby-talk@ruby-lang.org
Subject: Re: How to delete read-only files with Ruby commands

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.

Thanks!

-Kurt Euler

Try the standard library file ‘fileutils.rb’. You can RDoc the file to
see some documentation or (of course) look at the file directly.

From memory:

require ‘fileutils’

FileUtils.rm_rf(directory) # trash a directory tree
FileUtils.cp(src, dest, :force => true) # copy over a RO file

These use Unix names, but I imagine it will work on Windows.

Gavin