RUBY写的 目录及文件复制代码
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 |
require 'find' require 'fileutils' # usage: dir_move target-path (,local-path ) # it will creat a directory if target-path is empty # TODO: add a option for u to choose whether replace everyting # if local and target has same name but not the same type(file,directory) def dir_move( target,dirpath=Dir.pwd) ## check if path valid if !File.directory?(dirpath) puts "error first path" return elsif File.file?(target) puts "error :target is a file exist" return end ### Dir.mkdir target unless File.exist? target Find.find dirpath do |path| path_lite=path.gsub(dirpath,'') target_path=target+path_lite begin if File.directory? path Dir.mkdir target_path else FileUtils.copy path,target_path end rescue if type(path) != type(target_path) puts "waring: type unmatch, local=>#{path} is a #{type(path)},target=>#{target_path} is a #{type(target_path)}" end end end puts "everyting is ok" end def type(file) return unless File.exist? file if File.file? file "file" else "directory" end end |
没怎么严格测试。嘿嘿
Posted by xhan At August 13, 2008 17:56
请登录以发表评论。