Created
May 4, 2014 13:37
-
-
Save ttscoff/11516751 to your computer and use it in GitHub Desktop.
A script to create nested folders from a flat folder containing files with configured prefixes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'yaml' | |
require 'fileutils' | |
class Folderize | |
def initialize(config_file=false) | |
config_file = File.expand_path("~/.folderize") unless config_file | |
# Set up config | |
unless File.exists?(config_file) | |
new_config = { | |
'flat_folder' => "/path/to/flat/notes/folder", | |
'nested_folder' => "/path/to/root/nested/folder", | |
'prefixes' => {'xxx' => 'Ideas', 'yyy' => 'Notes'} | |
} | |
File.open(config_file, 'w') { |yf| YAML::dump(new_config, yf) } | |
$stdout.puts "Sample config written to #{config_file}" | |
$stdout.puts "Please edit the config file and run again" | |
Process.exit 0 | |
end | |
config = YAML.load_file(config_file) | |
@flat_folder = File.expand_path(config['flat_folder']) | |
@nested_folder = File.expand_path(config['nested_folder']) | |
@prefixes = config['prefixes'] | |
# verify config | |
if @flat_folder == "/path/to/flat/notes/folder" || @nested_folder == "/path/to/root/nested/folder" | |
puts "Please edit #{config_file} and run again" | |
Process.exit 1 | |
end | |
unless File.directory?(@flat_folder) | |
FileUtils.mkdir_p(@flat_folder) | |
end | |
@prefixes.each {|pre, folder| | |
unless File.directory?(File.join(@nested_folder, folder)) | |
FileUtils.mkdir_p(File.join(@nested_folder, folder)) | |
end | |
} | |
end | |
def sync_all | |
nested_files = Dir.glob(File.join(@nested_folder,"**","*")) | |
flat_files = Dir.glob(File.join(@flat_folder,"*")) | |
nested_files.each {|file| sync(file) } | |
flat_files.each {|file| sync(file) } | |
end | |
def sync(file) | |
from_file = File.expand_path(file) | |
to_file = false | |
if File.dirname(file) == @flat_folder | |
@prefixes.each {|pre,folder| | |
if File.basename(from_file) =~ /^#{pre}/ | |
to_file = File.join(@nested_folder, folder, File.basename(from_file).sub(/^#{pre}\s*/, '')) | |
break | |
end | |
} | |
else | |
@prefixes.each {|pre,folder| | |
if File.dirname(from_file).split(/\//)[-1] == folder | |
to_file = File.join(@flat_folder, "#{pre} #{File.basename(from_file)}") | |
break | |
end | |
} | |
end | |
return unless to_file | |
if File.exists?(to_file) | |
if File.stat(from_file).mtime > File.stat(to_file).mtime | |
FileUtils.copy_file(from_file,to_file,true) | |
end | |
else | |
FileUtils.copy_file(from_file,to_file,true) | |
end | |
end | |
end | |
f = Folderize.new | |
if ARGV.length > 0 | |
ARGV.each {|file| | |
if File.exists?(File.expand_path(file)) | |
f.sync(file) | |
else | |
raise "No such file: #{file}" | |
end | |
} | |
else | |
f.sync_all | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment