Skip to content

Instantly share code, notes, and snippets.

@dented
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save dented/fb038b8e39503134b7c8 to your computer and use it in GitHub Desktop.

Select an option

Save dented/fb038b8e39503134b7c8 to your computer and use it in GitHub Desktop.
Move a tree of files in a single folder to a new list of folders with individual files
require 'fileutils'
directory = './files'
new_directory = './new'
files = Dir["#{directory}/*"]
Dir.mkdir(new_directory) if !Dir.exists?(new_directory)
files.each do |file|
file_name = File.basename(file)
directory_name = File.basename(file_name, File.extname(file_name))
directory_path = "#{new_directory}/#{directory_name}"
Dir.mkdir(directory_path) if !Dir.exists?(directory_path)
FileUtils.mv(file, "#{directory_path}/#{file_name}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment