Files/Folders Iterating through and moving files around conditionally

Hi,

I’m struggling with what should be a fairly simple concept, but I’m
somewhat new at this. I’m trying to iterate through an array of
folders, change location to each directory, acknowledge the files within
that directory, then move the files to subdirectories that exist if and
when the filename contains the subdirectory name.

So root has:

a/
b/
c/
d/

Then each folder has
abcdef-CatName01-abcdef.xml
ghijkl-CatName02-ghijkl.xml
mnop-Catname03-mnop.xml
CatName01/
CatName02/
CatName03/

And I want to move the files into folders so that if there’s a CatName01
file, put it into CatName01 folder.

My method takes in 2 existing arrays, instruments[] and cats[], where
instruments are the folders in the root and cats are the folders in the
instruments folders.

I have this which doesn’t quite work.

def ins_file_move(instruments, cats)

instruments.each do |current_dir|
Dir.chdir(current_dir)
source = Dir.glob(’*.xml’)
cats.each do |dest_cats|
if source.glob(current_dir)
Dir.mv("#{source}", “#{cats}/#{source}”)
end
end
Dir.chdir("…")
end

end
ins_file_move(instruments, cats)

It looks like I can’t glob a glob. I tried match and scan but it’s
still treating current_dir as an array through the 2nd each loop.

Can anyone recommend a way to successfully do this? Either using my
existing approach or something different? I’m guessing there’s probably
a more eloquent way of iterating through an iteration where both are
variables.

Thanks!

a/
CatName02/
I have this which doesn’t quite work.
end

Can anyone recommend a way to successfully do this? Either using my
existing approach or something different? I’m guessing there’s probably
a more eloquent way of iterating through an iteration where both are
variables.

Thanks!

There are several issues:

  1. yes, glob doesn’t work with an array, use a regex match
  2. your ‘source’ and ‘cats’ are arrays of files, you can’t mv arrays
  3. mv is a FileUtils method, not a Dir method
  4. you have to check each source file to see if it contains the
    destination folder, you are trying to check the whole array at once, you
    need another loop

This code should do what you want.

require ‘fileutils’
def ins_file_move(instruments, cats)

instruments.each do |current_dir|
Dir.chdir(current_dir)
source = Dir.glob(’*.xml’)
cats.each do |dest_cats|
source.each do |source_file|
if source_file =~ /#{dest_cats}/
FileUtils.mv("#{source_file}", “#{dest_cats}/#{source_file}”)
end
end
end
Dir.chdir("…")
end
end

Michael Hansen

Thank you! I was closer than I thought, but I see your points now. I
had to make one change based on my directory structure which I didn’t
catch before.

FileUtils.mv("#{source_file}",
“#{current_dir}–#{dest_cats}/#{source_file}”)

I appreciate the explanation.
Thanks
Mike

require ‘fileutils’
def ins_file_move(instruments, cats)

instruments.each do |current_dir|
Dir.chdir(current_dir)
source = Dir.glob(’*.xml’)
cats.each do |dest_cats|
source.each do |source_file|
if source_file =~ /#{dest_cats}/
FileUtils.mv("#{source_file}", “#{dest_cats}/#{source_file}”)
end
end
end
Dir.chdir("…")
end
end

Michael Hansen