HT delete all files in a directory

Rubites:

Newbie question: What’s the Ruby command to unconditionally delete all files in a directory?

Thanks!

-Kurt

Kurt Euler wrote:

Rubites:

Newbie question: What’s the Ruby command to unconditionally delete all
files in a directory?

Thanks!

-Kurt

rm -Rf *
That should do the job. Are you sure that’s what you want? (The
backquote is similar to the shell backquote.)
P.S.: I haven’t tried this. If you really are thinking of doing it, I
would definitely recommend that you use a full path name rather than
just a glob.

···


– Charles Hixson
Gnu software that is free,
The best is yet to be.

I use this:

require ‘find’

class Dir
def Dir.visit(dir = ‘.’, files_first = false, &block)
if files_first
paths =
Find.find(dir) { |path| paths << path }
paths.reverse_each {|path| yield path}
else
Find.find(dir, &block)
end
end

def Dir.rm_rf(dir)
Dir.visit(dir, true) do |path|
if FileTest.directory?(path)
Dir.unlink(path)
else
File.unlink(path)
end
end
end
end

Massimiliano

···

On Wed, Aug 07, 2002 at 06:08:37AM +0900, Kurt Euler wrote:

Newbie question: What’s the Ruby command to unconditionally delete
all files in a directory?

rm -Rf *
That should do the job. Are you sure that’s what you want? (The
backquote is similar to the shell backquote.)
P.S.: I haven’t tried this. If you really are thinking of doing
it, I
would definitely recommend that you use a full path name rather
than
just a glob.

This won’t work on Windows w/o cygwin (or similar) installed. Too,
some unixen use ‘-r’ (lower case only) for recursive.

···

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do You Yahoo!?
Yahoo! Health - Feel better, live better

unlink won’t delete read-only files either (at least on windows it
won’t), so a File.chmod( 0644, fname) will be necessary before the
unlink call.