Help with moving files

  • Thread starter Jeremy Woertink
  • Start date
J

Jeremy Woertink

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
 
J

Jer

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

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

Jer

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?
 
J

Jeremy Heiler

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?

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

Jeremy Woertink

Jeremy said:
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
 
J

Jeremy Woertink

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top