Class: Ld::Dir

Inherits:
Object
  • Object
show all
Defined in:
lib/ld/file/dir.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, dir = nil) ⇒ Dir

Returns a new instance of Dir.



5
6
7
8
9
10
# File 'lib/ld/file/dir.rb', line 5

def initialize path, dir = nil
  @path = path
  @dir = dir
  @name = File.basename path
  iter
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def dir
  @dir
end

#dirsObject

Returns the value of attribute dirs.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def dirs
  @dirs
end

#filesObject

Returns the value of attribute files.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def files
  @files
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def name
  @name
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def path
  @path
end

Instance Method Details

#create_dir(name) ⇒ Object

创建文件夹



57
58
59
# File 'lib/ld/file/dir.rb', line 57

def create_dir name
  Dir.mkdir "#{@path}/#{name}"
end

#create_file(name) ⇒ Object

创建文件



52
53
54
# File 'lib/ld/file/dir.rb', line 52

def create_file name
  File.new "#{@path}/#{name}"
end

#destroy_allObject

删除文件夹以及其下所有文件与目录



47
48
49
# File 'lib/ld/file/dir.rb', line 47

def destroy_all
  # FileUtils.rm_r path
end

#files_countObject

查看当前目录文件总大小



68
69
70
71
72
73
74
# File 'lib/ld/file/dir.rb', line 68

def files_count
  sum = 0
  files.each do |file|
    sum += file.size
  end
  sum
end

#files_info(i = 0) ⇒ Object

打印当前目录下所有文件夹的信息



109
110
111
112
113
114
# File 'lib/ld/file/dir.rb', line 109

def files_info(i = 0)
  files.each do |file|
    file.info(i + 1)
  end
  puts
end

#info(i = 0) ⇒ Object

打印当前目录的汇总信息



117
118
119
120
121
# File 'lib/ld/file/dir.rb', line 117

def info(i = 0)
  print "\t" * i
  puts "目录#{path}下:子文件夹个数#{dirs.size},子文件个数#{files.size}"
  puts "总文件个数#{sum_files_count},当前文件夹大小#{files_count/1024.round(3)}K,文件夹总大小#{sum_files_size}K"
end

#info_all(i = 0) ⇒ Object

迭代打印当前目录下所有文件与文件夹的信息



98
99
100
101
102
103
104
105
106
# File 'lib/ld/file/dir.rb', line 98

def info_all(i = 0)
  info(i)
  files_info(i)
  i += 1
  dirs.each_with_index do |dir,k|
    dir.info_all(i)
  end
  nil
end

#iterObject

迭代记录所有目录与文件



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ld/file/dir.rb', line 13

def iter
  @dirs  = []
  @files = []
  Dir.foreach path do |p|
    if !['.','..','.DS_Store'].include?(p)
      if File.directory? "#{path}/#{p}"
        @dirs << Ld::Dir.new("#{path}/#{p}", self)
      else
        @files << Ld::File.new("#{path}/#{p}", self)
      end
    end
  end
end

#iter_allObject

手动



28
29
30
31
32
# File 'lib/ld/file/dir.rb', line 28

def iter_all
  iter.dirs.each do |dir2|
    dir2.iter_all
  end
end

#mkdir(name) ⇒ Object

创建文件夹



42
43
44
# File 'lib/ld/file/dir.rb', line 42

def mkdir name
  Dir.mkdir "#{path}/#{name}"
end

#mv_to(dir) ⇒ Object

移动文件夹



35
36
37
38
39
# File 'lib/ld/file/dir.rb', line 35

def mv_to dir
  files.each do |file|
    file.mv dir
  end
end


61
62
63
64
65
# File 'lib/ld/file/dir.rb', line 61

def print_tree
  dirs.each do |dir|
    puts "#{dir.name}"
  end
end

#sum_files_count(count = 0) ⇒ Object

查看当前目录下所有文件个数



77
78
79
80
81
82
83
84
# File 'lib/ld/file/dir.rb', line 77

def sum_files_count count = 0
  @@sum_count = count + files.size
  # puts "#{@@sum_count}\t+=\t#{count}\t+\t#{files.size}"
  dirs.each do |dir|
    dir.sum_files_count @@sum_count
  end
  @@sum_count
end

#sum_files_size(size = 0) ⇒ Object

查看当前目录下所有文件个数



87
88
89
90
91
92
93
94
# File 'lib/ld/file/dir.rb', line 87

def sum_files_size size = 0
  @@sum_size = size + files_count
  # puts "#{@@sum_size}\t+=\t#{size}\t+\t#{files_count/1024.round(3)} (#{name})"
  dirs.each do |dir|
    dir.sum_files_size @@sum_size
  end
  @@sum_size
end