-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflatten_directories.rb
45 lines (39 loc) · 1.05 KB
/
flatten_directories.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# ruby flatten_directories.rb "/home/anton/root_dir"
# Flatten all the child directories in the folder "root_dir"
require 'find'
require 'fileutils'
require 'pathname'
def for_each_child_directory(root_dir)
return if !block_given?
old_dir = Dir.pwd
Dir.chdir root_dir
Dir.glob("*") do |file_path|
if File.directory?(file_path)
yield file_path
end
end
Dir.chdir old_dir
end
def move_all_new_files_from_subdirectories_to(dir)
Find.find(dir) do |file_path|
already_exists = File.exist?(File.join(dir, Pathname.new(file_path).basename))
if !File.directory?(file_path) && !already_exists
FileUtils.mv(file_path, dir)
end
end
end
def remove_all_subdirectories_in(dir)
for_each_child_directory(dir) do |dir|
FileUtils.rm_rf dir
end
end
def flatten_directory(dir)
move_all_new_files_from_subdirectories_to(dir)
remove_all_subdirectories_in(dir)
end
def flatten_child_directories(root_dir)
for_each_child_directory(root_dir) do |dir|
flatten_directory dir
end
end
flatten_child_directories(ARGV[0] || ".")