From: "Michael T. Richter" Date: 2007-06-09T12:30:42+09:00 Subject: Re: Newb Wants to Better His File, Dir code --=-vMCtuXXYfwbKz3l93lJ+ Content-Type: multipart/alternative; boundary="=-UaO/6anxY9D+Xm+BpanR" --=-UaO/6anxY9D+Xm+BpanR Content-Type: text/plain Content-Transfer-Encoding: quoted-printable 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=20 > better way. Mine seems clumsy. >=20 > My goal is to search over all files, identify all that are directories=20 > and then do something to each directory. I don't need to go into=20 > directories recursively yet. >=20 > The fact that Dir.foreach returns strings seems odd to me. All advice=20 > appreciated. >=20 >=20 > #!/usr/local/bin/ruby >=20 > dpath =3D Dir.getwd >=20 > Dir.foreach(dpath) do > |x| > ddir =3D File.join(dpath,x) > if File.directory?(ddir) && x !=3D "." && x !=3D ".." then > puts %x{du -sh #{x}} > end > end Try: dirs =3D Dir.entries(".").reject{|e| /\.{1,2}/ =3D~ 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. --=20 Michael T. Richter (GoogleTalk: ttmrichter@gmail.com) I can see computers everywhere - except in the productivity statistics! (Robert Solow) --=-UaO/6anxY9D+Xm+BpanR Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 hop=
ing 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 n=
eed 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 =3D Dir.getwd

Dir.foreach(dpath) do
  |x|
  ddir =3D File.join(dpath,x)
  if File.directory?(ddir) && x !=3D "=
." && x !=3D ".." then
    puts %x{du -sh #{x}}
  end
end

Try:

dirs =3D Dir.entries(".").reject{|e| /\.{1,2}/ =3D~ e}.fi= nd_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.

--
Michael T. Richter <ttmri= chter@gmail.com> (GoogleTalk: ttmrichter@gmail.com)
I can see computers everywhere - except in the productivity statistics! = (Robert Solow)
--=-UaO/6anxY9D+Xm+BpanR-- --=-vMCtuXXYfwbKz3l93lJ+ Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) iD8DBQBGah7XLqyWkKVQ54QRAoUMAKCPiX5Hlwbx+ffA1UFcjty19w9tkQCg2qy7 hHQ+OqpH3m/ikQrJPz5B3Us= =JwqZ -----END PGP SIGNATURE----- --=-vMCtuXXYfwbKz3l93lJ+--