Class: Dir

Inherits:
Object show all
Defined in:
lib/rwd/ftools.rb

Class Method Summary collapse

Class Method Details

.copy(from, to) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rwd/ftools.rb', line 4

def self.copy(from, to)
  if File.directory?(from)
    pdir	= Dir.pwd
    todir	= File.expand_path(to)

    File.mkpath(todir)

    Dir.chdir(from)
      Dir.new(".").each do |e|
        Dir.copy(e, todir+"/"+e)	if not [".", ".."].include?(e)
      end
    Dir.chdir(pdir)
  else
    todir	= File.dirname(File.expand_path(to))

    File.mkpath(todir)

    File.copy(from, to)
  end
end

.find(entry = nil, mask = nil) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rwd/ftools.rb', line 56

def self.find(entry=nil, mask=nil)
  entry	= "."	if entry.nil?

  entry	= entry.gsub(/[\/\\]*$/, "")	unless entry.nil?

  mask	= /^#{mask}$/i	if mask.kind_of?(String)

  res	= []

  if File.directory?(entry)
    pdir	= Dir.pwd

    res += ["%s/" % entry]	if mask.nil? or entry =~ mask

    begin
      Dir.chdir(entry)

      begin
        Dir.new(".").each do |e|
          res += Dir.find(e, mask).collect{|e| entry+"/"+e}	unless [".", ".."].include?(e)
        end
      ensure
        Dir.chdir(pdir)
      end
    rescue Errno::EACCES => e
      $stderr.puts e.message
    end
  else
    res += [entry]	if mask.nil? or entry =~ mask
  end

  res
end

.move(from, to) ⇒ Object


25
26
27
28
# File 'lib/rwd/ftools.rb', line 25

def self.move(from, to)
  Dir.copy(from, to)
  Dir.rm_rf(from)
end

.rm_rf(entry) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rwd/ftools.rb', line 30

def self.rm_rf(entry)
  File.chmod(0755, entry)

  if File.ftype(entry) == "directory"
    pdir	= Dir.pwd

    Dir.chdir(entry)
      Dir.new(".").each do |e|
        Dir.rm_rf(e)	if not [".", ".."].include?(e)
      end
    Dir.chdir(pdir)

    begin
      Dir.delete(entry)
    rescue => e
      $stderr.puts e.message
    end
  else
    begin
      File.delete(entry)
    rescue => e
      $stderr.puts e.message
    end
  end
end