On Oct 28, 1:40=A0pm, Axel Etzold <AEtz... / gmx.de> wrote:
> -------- Original-Nachricht --------
>
>
>
> > Datum: Wed, 29 Oct 2008 02:53:09 +0900
> > Von: Bee Tard <sca... / gmail.com>
> > An: ruby-t... / ruby-lang.org
> > Betreff: createing directories and moving files into it
> > I have an undetermined list of directories.
>
> > I am trying to loop through them all and in each one I want to make a
> > directory called 'images'. when i do this, I want o move all the imagge=
s
> > in that folder into it.
>
> > so i have a tree like this:
>
> > some-dir
> > \_abc
> > =A0- img00971273.jpg
> > =A0- img21234235.jpg
> > =A0- img12345623.jpg
> > \_123
> > =A0- img99554361.jpg
> > =A0- img21234235.jpg
> > =A0- img53840534.jpg
> > \_xyz
> > =A0- img00930443.jpg
> > =A0- img12364235.jpg
> > =A0- img09982623.jpg
> > \_890
> > =A0- img00923871.jpg
> > =A0- img21292835.jpg
> > =A0- img08836823.jpg
>
> > I would like to create
>
> > some-dir
> > \_abc
> > =A0\__images
> > =A0 =A0- img00971273.jpg
> > =A0 =A0- img21234235.jpg
> > =A0 =A0- img12345623.jpg
>
> > And so on and so forth.
>
> > Hope this illustration makes sense. I have tried to create a loop and g=
o
> > through each directory and use system() to 'mkdir images' and mv *.jpg
> > /images
>
> > no luck as yet. really starting to like some of Ruby's fileutils. just
> > wish i could get moving with some of these tasks :)
>
> > All help greatly appreciated. I'll buy you a beer at lunch.
> > --
> > Posted viahttp://www.ruby-forum.com/.
>
> Hi ---
>
> you can create a directory, if it doesn't yet exist, using fileutils:
>
> irb(main):001:0> require "fileutils"
> =3D> true
> irb(main):002:0> FileUtils.mkdir_p "temp5"
> =3D> "temp5"
>
> If you do it again, that doesn't matter.
> For moving files, I like the rio gem:
>
> http://rio.rubyforge.org/
>
> require "rubygems"
> require "rio"
>
> Iterate over the .rb files in a directory and its subdirectories.
>
> =A0rio('adir').all.files('*.rb') { |entrio|
>
> =A0 =A0# do some checks on entrio.to_s to classify things
>
> =A0 if /\.jpg$/.match(entrio.to_s)
> =A0 =A0 p "it's an image
> =A0 end
>
> =A0}
>
> Do =A0also check that you have writing permissions on the directory you w=
ant to write to.
>
> Best regards,
>
> Axel
> --
> Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
> Ideal f=FCr Modem und ISDN:http://www.gmx.net/de/go/smartsurfer

Using Rio's rename-mode:

require 'rio'

# This will work if the only images under 'some-dir' are
# as stated in the problem

rio('some-dir').rename.all.files('*.jpg') do |ifile|
  ifile.dirname =3D rio(ifile.dirname,'images').mkdir
end

# This is little more robust

rio('some-dir').dirs do |d|
  d.rename.files('*.jpg') do |ifile|
    ifile.dirname =3D rio(ifile.dirname,'images').mkdir
  end
end