Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Last active December 20, 2015 17:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huacnlee/6171561 to your computer and use it in GitHub Desktop.
Save huacnlee/6171561 to your computer and use it in GitHub Desktop.
将一个目前下面的文件,根据文件名前缀分散到不同的文件夹,比如 a12.jpg -> a/1/a12.jpg, b287.jpg -> b/2/b287.jpg 此方法用于处理由于最初设计上传文件夹没有分层次导致一个目录文件过多的问题。可以将一个目录下面的文件分散到子目录中
require "fileutils"
root_dir = "/Users/jason/Downloads/images"
Dir.chdir(root_dir)
puts Dir.pwd
Dir.glob("**/*.{jpg}").each do |fname|
tfname = fname.split("/").last
if fname.match("[small|large|normal]_")
tfname = fname.split("_").last
end
p1,p2 = tfname[0,1],tfname[1,1]
dir_path = [root_dir,p1,p2].join("/")
if !Dir.exist?(dir_path)
FileUtils.mkdir_p(dir_path)
end
FileUtils.mv(fname,"#{dir_path}/")
print "."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment