复制文件并去除特殊字符文件名

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
require 'find'
require 'fileutils'

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).gsub(/[^a-zA-Z0-9\.\-\+_\(\)\/]/,"_").gsub(/(__)+/,"zhongwen")
        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

dir_move("/Users/holin/bin2", "/Users/holin/bin")

    Posted by holin At October 15, 2008 16:54

请登录以发表评论。