[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Removing files in directtories

John Maclean

2/18/2006 12:21:00 AM

Almost there... I've included line 7 as I'd like to be able to remove
files from a directory using code blocks. Commented it out for now
until I can work out how it's done. Suggestions appreciated.

#!/usr/bin/ruby -w
def back_up
backdir = ENV["HOME"] + "/foo/"
puts "about to backup some files and tar them into ~/foo"
puts "listing data in ~/foo"
Dir.foreach(backdir) {|file_names|
# File.delete("#{backdir}" + file_names)
puts File.directory?
file_names }
end
back_up

--
John Maclean
MSc (DIC)
07739 171 531



3 Answers

William James

2/18/2006 6:55:00 AM

0

John Maclean wrote:
> Almost there... I've included line 7 as I'd like to be able to remove
> files from a directory using code blocks. Commented it out for now
> until I can work out how it's done. Suggestions appreciated.
>
> #!/usr/bin/ruby -w
> def back_up
> backdir = ENV["HOME"] + "/foo/"
> puts "about to backup some files and tar them into ~/foo"
> puts "listing data in ~/foo"
> Dir.foreach(backdir) {|file_names|
> # File.delete("#{backdir}" + file_names)
> puts File.directory?
> file_names }
> end
> back_up
>
> --
> John Maclean
> MSc (DIC)
> 07739 171 531

def back_up
backdir = ENV["HOME"] + "/foo/"
puts "about to backup some files and tar them into ~/foo"
puts "listing data in ~/foo"

Dir.foreach(backdir) { |name|
name = File.join( backdir, name )
if File.file?( name )
yield name
end
}
end

back_up { |file_name|
puts file_name
}

Glen

2/19/2006 3:23:00 AM

0

I wrote the following empty method as an extension to the builtin Dir
class. It iterates through a directory deleting all files and
directories, leaving the top level directory untouched.

class Dir

def empty()
self.each { |f|
if f !~ /^(\.{1,2})$/
file = File.join(self.path, f)

if File.stat(file).file?
File.delete(file) rescue nil;
elsif File.stat(file).directory?
d = Dir.new(file)
d.empty()
Dir.delete(file) rescue nil;
end
end
}
end

end

Robert Klemme

2/19/2006 10:43:00 AM

0

John Maclean <info@jayeola.org> wrote:
> Almost there... I've included line 7 as I'd like to be able to remove
> files from a directory using code blocks. Commented it out for now
> until I can work out how it's done. Suggestions appreciated.
>
> #!/usr/bin/ruby -w
> def back_up
> backdir = ENV["HOME"] + "/foo/"
> puts "about to backup some files and tar them into ~/foo"
> puts "listing data in ~/foo"
> Dir.foreach(backdir) {|file_names|
> # File.delete("#{backdir}" + file_names)
> puts File.directory?
> file_names }
> end
> back_up

I'm not sure what you want, but you might want to consider this:

printing:
ruby -r find -e 'Find.find( File.join( ENV["HOME"], "foo" ) ) {|f| puts f}'

deletion:
ruby -r fileutils -e 'FileUtils.rm_rf( File.join( ENV["HOME"], "foo" ) )'

Kind regards

robert