From: michael libby Date: 2002-09-23T12:37:47+09:00 Subject: Re: Getting a directory tree -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sunday 22 September 2002 16:34, dblack@candle.superlink.net wrote: > On Mon, 23 Sep 2002, Hal E. Fulton wrote: > > Here's one new version. It DOES NOT work, and I'm > > not sure why yet. > > > > def getdirs1(root) > > dirs=Dir.entries(root) - [".", ".."] > > temp = [] > > dirs.each {|x| if test(?d,x) then temp << (root+SEP+x) end } > > list = [root] + temp > > dirs.each {|x| list += getdirs(x) } > > list > > end > > > > So my two questions are: > > 1. What's wrong with getdirs1 that I'm not seeing? The same problem David has below. The routine doesn't handle recursive directories well-- it follows the link back up the tree. You know that odd case where some malcontent has done: ln -s ../.. link_up In fact, I was surprised my test with David's routine below ever returned, but believe me... it was the ugliest nested list you ever saw. > > 2. How would you write this in a reasonably > > efficient way? Add a check for symlinks. > def getdirs_d1(root) return [] if File.lstat(root).symlink? > dirs=Dir.entries(root) - [".", ".."] > temp = dirs.map {|d| root+SEP+d}.select {|d| test(?d,d)} > temp + temp.map {|x| getdirs1(x) } > end -michael ++++++++++++++++++++++++++++++++++++++++++ Michael C. Libby public key: http://www.ichimunki.com/public_key.txt web site: http://www.ichimunki.com ++++++++++++++++++++++++++++++++++++++++++ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE9jouN4ClW9KMwqnMRAinXAJ94wOXrxS51J7YiAdb0YjA9tsblYQCfS2bR yuzhCezve8ViwQmsC5gFltQ= =a2AB -----END PGP SIGNATURE-----