I have been putting off the chore of either a) finding a script or program to move and rename my files b) writing a script to move and rename files. Tonight I went with plan b. I am sure there is something out there for plan a, I just couldn’t find it quick enough for me to not go ahead with plan b. Loading up irb I used ruby with the help of the FileMagick gem. Below you will see a not so elegant way to copy a set of files in ruby to the parent directory.
#navigate to the directory of where your files exsit
#load irb
#int inew directory
d=Dir.new('.')
#require the following libs
require 'filemagic'
require 'ftools'
# loop directories
d.entries.each do |f|
if fm.file(f.to_s).eql? 'directory'
f.each do |sub|
next if sub =~ /^\.\.?$/
dh = Dir.open(sub)
Dir.chdir(sub.to_s) # enter subdirectory
puts Dir.pwd
dh.each do |file|
next if file =~ /^\.\.?$/
puts "Copy File: #{file.to_s}, ../#{sub}_#{file}"
File.copy(file,"../#{sub}_#{file}") # copy file to parent directory
end
Dir.chdir("..")
puts Dir.pwd
end
end
end
Related: http://pleac.sourceforge.net/pleac_ruby/directories.html