[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to delete read-only files with Ruby commands

Kurt Euler

10/8/2003 6:11:00 AM

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

3 Answers

Hal E. Fulton

10/8/2003 6:17:00 AM

0

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


Gavin Sinclair

10/8/2003 6:42:00 AM

0

> 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



Chris Morris

10/8/2003 1:16:00 PM

0

Gavin Sinclair wrote:

> 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.
>
Looks like it will (remove_file is ultimately called by rm_rf and
variants...)

def remove_file( fname, force = false ) #:nodoc:
first_time_p = true
begin
File.unlink fname
rescue Errno::ENOENT
raise unless force
rescue
if first_time_p
# try once more for Windows
first_time_p = false
File.chmod 0777, fname
retry
end
raise
end
end

A slightly more direct version, which I know is good for Windows, but
not verified on *nix[1]:

files.each do |file|
# make writable to allow deletion
File.chmod(0644, file)
File.delete(file)
end

[1] from http://www.clabs.org/...

--

Chris
http://clabs....