# Help with moving files

**URL:** <https://www.ruby-forum.com/t/help-with-moving-files/105748>\
**Category:** Ruby\
**Created:** [June 2, 2007, 1:59am UTC](https://www.ruby-forum.com/t/help-with-moving-files/105748 "2007-06-02T01:59:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [June 2, 2007, 1:59am UTC](https://www.ruby-forum.com/t/help-with-moving-files/105748/1 "2007-06-02T01:59:56Z")

</div>

I am in need of some help with moving some files. I have some folders  
that are inside an images directory, and inside each of the these  
directories there are images that I want to move to another folder so  
that all the images are all in the same folder.

Here’s what I got so far, one problem is when I call photos, it looks at  
the directory where the script is held, but I want it to look at the  
directory the images are in.

require ‘fileutils’

current\_dir = Dir.pwd

photos = Dir.glob("\*.{jpg,gif}")

directories = Array.new  
Dir.new(Dir.pwd).entries.each { |dir| directories.push(dir) if  
File.directory?(dir) }

directories.each do |dir|  
next if dir.eql?(".")  
next if dir.eql?("…")  
photos.each do |pic|  
FileUtils.mv(pic, “C:\Scripts\folder 2”)  
end  
end

Any help is appricated

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [June 2, 2007, 3:41am UTC](https://www.ruby-forum.com/t/help-with-moving-files/105748/2 "2007-06-02T03:41:41Z")

</div>

On Jun 1, 7:59 pm, Jeremy W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> next if dir.eql?(“…”)  
> photos.each do |pic|  
> FileUtils.mv(pic, “C:\Scripts\folder 2”)  
> end  
> end
> 
> Any help is appricated
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

If Dir.pwd gives you the directory of the script, how would it know  
where the images directory is?

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [June 2, 2007, 4:11am UTC](https://www.ruby-forum.com/t/help-with-moving-files/105748/3 "2007-06-02T04:11:42Z")

</div>

On Jun 1, 9:37 pm, Jer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Here’s what I got so far, one problem is when I call photos, it looks at  
> > Dir.new(Dir.pwd).entries.each { |dir| directories.push(dir) if  
> > Any help is appricated
> 
> > –  
> > Posted viahttp://www.ruby-forum.com/.
> 
> If Dir.pwd gives you the directory of the script, how would it know  
> where the images directory is?

Also, your Dir.glob returns an array holding all your jpg or gif  
images. Therefore you don’t need to take them and put them into  
another array. Your script can be chomped down to three lines of code  
(including the require) or four if you use do…end instead of { and }.

require ‘fileutils’  
Dir.chdir(‘/Users/jeremy/Desktop/photos’)  
Dir.glob(“\*.{jpg,gif}”).each { |pic| FileUtils.mv(pic, “/Users/jeremy/  
Desktop”) }

Look through your code an compare it to mine. Do you see the  
differences?

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [June 2, 2007, 4:21am UTC](https://www.ruby-forum.com/t/help-with-moving-files/105748/4 "2007-06-02T04:21:43Z")

</div>

On Jun 1, 10:06 pm, Jer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > that all the images are all in the same folder.
> 
> > > end  
> > > images. Therefore you don’t need to take them and put them into  
> > > another array. Your script can be chomped down to three lines of code  
> > > (including the require) or four if you use do…end instead of { and }.
> 
> require ‘fileutils’  
> Dir.chdir(‘/Users/jeremy/Desktop/photos’)  
> Dir.glob(“\*.{jpg,gif}”).each { |pic| FileUtils.mv(pic, “/Users/jeremy/  
> Desktop”) }
> 
> Look through your code an compare it to mine. Do you see the  
> differences?

Okay, I feel like a doofus. My script only works for one directory. I  
didn’t re-read you entire message. My fault.

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [June 3, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/help-with-moving-files/105748/5 "2007-06-03T18:53:48Z")

</div>

Jeremy H. wrote:

> On Jun 1, 10:06 pm, Jer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > that all the images are all in the same folder.
> > 
> > > > end  
> > > > images. Therefore you don’t need to take them and put them into  
> > > > another array. Your script can be chomped down to three lines of code  
> > > > (including the require) or four if you use do…end instead of { and }.
> > 
> > require ‘fileutils’  
> > Dir.chdir(‘/Users/jeremy/Desktop/photos’)  
> > Dir.glob(“\*.{jpg,gif}”).each { |pic| FileUtils.mv(pic, “/Users/jeremy/  
> > Desktop”) }
> > 
> > Look through your code an compare it to mine. Do you see the  
> > differences?
> 
> Okay, I feel like a doofus. My script only works for one directory. I  
> didn’t re-read you entire message. My fault.

thanks for the idea. I knew the glob returned an array, I was thinking I  
might have to do some recursion so I stored that into the variable.  
Seems easy enough to move the files from a single directory to a single  
directory, but I need to move multiple files from multiple directories  
into a single directory, mainly it is on my server from  
current/public/images/event (where event is i.e. celine dion or blue man  
group etc…) to shared/public/images

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [June 3, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/help-with-moving-files/105748/6 "2007-06-03T19:58:06Z")

</div>

I think i’m getting closer, but I just need a little help getting over  
the top. I got

require ‘fileutils’

directories = Array.new  
Dir.new(Dir.pwd).entries.each { |dir| directories.push(dir) if  
File.directory?(dir) }

directories.each do |dir|  
next if dir.eql?(".")  
next if dir.eql?("…")  
Dir.chdir(dir)  
Dir.glob("\*.{jpg,gif}").each { |pic| FileUtils.mv(pic,  
“/shared/public/images”)}  
end

but still not quite right. Any other suggestions?
