# Createing directories and moving files into it

**URL:** <https://www.ruby-forum.com/t/createing-directories-and-moving-files-into-it/151988>\
**Category:** Ruby\
**Created:** [October 28, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/createing-directories-and-moving-files-into-it/151988 "2008-10-28T18:53:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![btard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@btard](https://www.ruby-forum.com/u/btard)\
**Post date:** [October 28, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/createing-directories-and-moving-files-into-it/151988/1 "2008-10-28T18:53:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![btard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@btard](https://www.ruby-forum.com/u/btard)\
**Post date:** [October 28, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/createing-directories-and-moving-files-into-it/151988/2 "2008-10-28T21:41:56Z")

</div>

-------- Original-Nachricht --------

> Datum: Wed, 29 Oct 2008 02:53:09 +0900  
> Von: Bee T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: createing directories and moving files into it

> - img00971273.jpg  
> \_890
> - img00971273.jpg  
> 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-forum.com/](http://www.ruby-forum.com/).

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.rubyforge.org/](http://rio.rubyforge.org/)

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

---

<div class="post-metadata">

**Author:** ![btard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@btard](https://www.ruby-forum.com/u/btard)\
**Post date:** [October 29, 2008, 12:34am UTC](https://www.ruby-forum.com/t/createing-directories-and-moving-files-into-it/151988/3 "2008-10-29T00:34:18Z")

</div>

Axel E. 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.rubyforge.org/](http://rio.rubyforge.org/)
> 
> 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.

---

<div class="post-metadata">

**Author:** ![btard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@btard](https://www.ruby-forum.com/u/btard)\
**Post date:** [October 29, 2008, 12:35am UTC](https://www.ruby-forum.com/t/createing-directories-and-moving-files-into-it/151988/4 "2008-10-29T00:35:13Z")

</div>

> 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)

---

<div class="post-metadata">

**Author:** ![btard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@btard](https://www.ruby-forum.com/u/btard)\
**Post date:** [October 29, 2008, 3:01am UTC](https://www.ruby-forum.com/t/createing-directories-and-moving-files-into-it/151988/5 "2008-10-29T03:01:40Z")

</div>

From: Bee T. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I did it this way:

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

you can do that too

system “for I in _; do mkdir ${I}/myFolder; mv ${I}/_ ${I}/myFolder;  
done”

i’m joking of course 😉

that is fine if you have only folders/directory (ie no regular files) in  
your current dir, and your folders contain just images.

in a full-fledged language, your checking is flexible, and your code is  
portable. plus you get the power of ruby.

anyway, try eg,

`
require 'fileutils'
Dir.glob("tempf/*").select{|x| FileTest.directory?(x)}.each{|dir|
  dest_folder = File.join(dir,"images")
  srce_files = Dir.glob(File.join(dir,"*.jpg"))
  Dir.mkdir dest_folder
  FileUtils.mv srce_files, dest_folder
}
`

note, tempf is just my sample test folder to test the code above.

---

<div class="post-metadata">

**Author:** ![btard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@btard](https://www.ruby-forum.com/u/btard)\
**Post date:** [November 9, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/createing-directories-and-moving-files-into-it/151988/6 "2008-11-09T17:10:47Z")

</div>

On Oct 28, 1:40 pm, Axel E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I am trying to loop through them all and in each one I want to make a  
> > \_123
> > 
> > - img08836823.jpg  
> > And so on and so forth.  
> > Posted viahttp://www.ruby-forum.com/.  
> > If you do it again, that doesn’t matter.
> 
> Best regards,
> 
> ## Axel
> 
> Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!  
> Ideal für Modem und ISDN:[Handytarif Vergleich 2023 | alle Anbieter vergleichen | GMX](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 = 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
