From: Dan Zwell Date: 2007-06-09T12:42:10+09:00 Subject: Re: Newb Wants to Better His File, Dir code Michael T. Richter wrote: > On Sat, 2007-09-06 at 11:35 +0900, R.B.Love wrote: > >> I've got code working to do what I want but I'm hoping there is a >> better way. Mine seems clumsy. >> >> My goal is to search over all files, identify all that are directories >> and then do something to each directory. I don't need to go into >> directories recursively yet. >> >> The fact that Dir.foreach returns strings seems odd to me. All advice >> appreciated. >> >> >> #!/usr/local/bin/ruby >> >> dpath = Dir.getwd >> >> Dir.foreach(dpath) do >> |x| >> ddir = File.join(dpath,x) >> if File.directory?(ddir) && x != "." && x != ".." then >> puts %x{du -sh #{x}} >> end >> end > > > Try: > > > dirs = Dir.entries(".").reject{|e| /\.{1,2}/ =~ e}.find_all{|e| > File.directory? e} > > > At this point you have a list of directory names. You can do whatever > you like to that list using .each(), .inject(), .collect(), etc. > The above has a small bug: it rejects all hidden directories. If you want to block only ".." and ".", then change it to: dirs = Dir.entries(".").reject{|e| /\.{1,2}$/ =~ e}.find_all{|e| File.directory? e}