[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

createing directories and moving files into it

Bee Tard

10/28/2008 5:53:00 PM

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 imagges
in that folder into it.

so i have a tree like this:

some-dir
\_abc
- img00971273.jpg
- img21234235.jpg
- img12345623.jpg
\_123
- img99554361.jpg
- img21234235.jpg
- img53840534.jpg
\_xyz
- img00930443.jpg
- img12364235.jpg
- img09982623.jpg
\_890
- img00923871.jpg
- img21292835.jpg
- img08836823.jpg

I would like to create


some-dir
\_abc
\__images
- img00971273.jpg
- img21234235.jpg
- img12345623.jpg

And so on and so forth.

Hope this illustration makes sense. I have tried to create a loop and go
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 via http://www.ruby-....

5 Answers

Axel Etzold

10/28/2008 8:41:00 PM

0


-------- Original-Nachricht --------
> Datum: Wed, 29 Oct 2008 02:53:09 +0900
> Von: Bee Tard <scappa@gmail.com>
> An: ruby-talk@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 imagges
> in that folder into it.
>
> so i have a tree like this:
>
> some-dir
> \_abc
> - img00971273.jpg
> - img21234235.jpg
> - img12345623.jpg
> \_123
> - img99554361.jpg
> - img21234235.jpg
> - img53840534.jpg
> \_xyz
> - img00930443.jpg
> - img12364235.jpg
> - img09982623.jpg
> \_890
> - img00923871.jpg
> - img21292835.jpg
> - img08836823.jpg
>
> I would like to create
>
>
> some-dir
> \_abc
> \__images
> - img00971273.jpg
> - img21234235.jpg
> - img12345623.jpg
>
> And so on and so forth.
>
> Hope this illustration makes sense. I have tried to create a loop and go
> 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 via http://www.ruby-....


Hi ---

you can create a directory, if it doesn't yet exist, using fileutils:

irb(main):001:0> require "fileutils"
=> true
irb(main):002:0> FileUtils.mkdir_p "temp5"
=> "temp5"

If you do it again, that doesn't matter.
For moving files, I like the rio gem:

http://rio.ruby...


require "rubygems"
require "rio"

Iterate over the .rb files in a directory and its subdirectories.

rio('adir').all.files('*.rb') { |entrio|

# do some checks on entrio.to_s to classify things

if /\.jpg$/.match(entrio.to_s)
p "it's an image
end

}

Do also check that you have writing permissions on the directory you want to write to.

Best regards,

Axel
--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/s...

Bee Tard

10/28/2008 11:34:00 PM

0

Axel Etzold wrote:
> you can create a directory, if it doesn't yet exist, using fileutils:
>
> irb(main):001:0> require "fileutils"
> => true
> irb(main):002:0> FileUtils.mkdir_p "temp5"
> => "temp5"
>
> If you do it again, that doesn't matter.
> For moving files, I like the rio gem:
>
> http://rio.ruby...
>
> require "rubygems"
> require "rio"
>
> Iterate over the .rb files in a directory and its subdirectories.
>
> rio('adir').all.files('*.rb') { |entrio|
>
> # do some checks on entrio.to_s to classify things
>
> if /\.jpg$/.match(entrio.to_s)
> p "it's an image
> end
>
> }
>
> Do also check that you have writing permissions on the directory you
> want to write to.
>

I did it this way:

for I in *; do mkdir ${I}/myFolder; mv ${I}/* ${I}/myFolder; done

was wondering forever about a ruby way. thanks for the help.
--
Posted via http://www.ruby-....

Bee Tard

10/28/2008 11:35:00 PM

0

> I did it this way:
>
> for I in *; do mkdir ${I}/myFolder; mv ${I}/* ${I}/myFolder; done
>
> was wondering forever about a ruby way. thanks for the help.

(in mac os terminal)
--
Posted via http://www.ruby-....

Peña, Botp

10/29/2008 1:29:00 AM

0

RnJvb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8L2NvZGU+DQoN
Cm5vdGUsIHRlbXBmIGlzIGp1c3QgbXkgc2FtcGxlIHRlc3QgZm9sZGVyIHRvIHRlc3QgdGhlIGNv
ZGUgYWJvdmUuDQo=

rio4ruby

11/9/2008 4:05:00 PM

0

On Oct 28, 1:40 pm, 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 imagges
> > in that folder into it.
>
> > so i have a tree like this:
>
> > some-dir
> > \_abc
> >  - img00971273.jpg
> >  - img21234235.jpg
> >  - img12345623.jpg
> > \_123
> >  - img99554361.jpg
> >  - img21234235.jpg
> >  - img53840534.jpg
> > \_xyz
> >  - img00930443.jpg
> >  - img12364235.jpg
> >  - img09982623.jpg
> > \_890
> >  - img00923871.jpg
> >  - img21292835.jpg
> >  - img08836823.jpg
>
> > I would like to create
>
> > some-dir
> > \_abc
> >  \__images
> >    - img00971273.jpg
> >    - img21234235.jpg
> >    - img12345623.jpg
>
> > And so on and so forth.
>
> > Hope this illustration makes sense. I have tried to create a loop and go
> > 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-....
>
> Hi ---
>
> you can create a directory, if it doesn't yet exist, using fileutils:
>
> irb(main):001:0> require "fileutils"
> => true
> irb(main):002:0> FileUtils.mkdir_p "temp5"
> => "temp5"
>
> If you do it again, that doesn't matter.
> For moving files, I like the rio gem:
>
> http://rio.ruby...
>
> require "rubygems"
> require "rio"
>
> Iterate over the .rb files in a directory and its subdirectories.
>
>  rio('adir').all.files('*.rb') { |entrio|
>
>    # do some checks on entrio.to_s to classify things
>
>   if /\.jpg$/.match(entrio.to_s)
>     p "it's an image
>   end
>
>  }
>
> Do  also check that you have writing permissions on the directory you want to write to.
>
> Best regards,
>
> Axel
> --
> Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
> Ideal für Modem und ISDN:http://www.gmx.net/de/go/s...

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 = 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 = rio(ifile.dirname,'images').mkdir
end
end