From: "Skye Shaw!@#$" Date: 2007-06-10T02:45:04+09:00 Subject: Re: Newb Wants to Better His File, Dir code On Jun 8, 8:42 pm, Dan Zwell wrote: > 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. > > 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} If hidden directories are not important, maybe try: [sshaw@localhost ~]$ ls -a . .emacs.d .ICEauthority .openoffice.org2.0 .. .esd_auth images .recently-used addr.pl .evolution java ruby ruby -le'Dir["/home/sshaw/*/"].each { |d| print d }' /home/sshaw/video/ /home/sshaw/me/ /home/sshaw/ruby/ /home/sshaw/java/ /home/sshaw/dos-mundos/ /home/sshaw/cti/ /home/sshaw/wb/ /home/sshaw/images/ /home/sshaw/music/ /home/sshaw/Desktop/ /home/sshaw/GDMX/ /home/sshaw/mysql/