Module: Ezfile
- Defined in:
- lib/ezfile.rb,
lib/ezfile.rb,
lib/ezfile/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
- .basename(file_name, suffix = "") ⇒ Object
- .copy_file(file_path, dest_file_path) ⇒ Object (also: copyfile)
- .dir_list(dir = "*") ⇒ Object (also: dirs)
- .ensure_permanently_delete_directory_and_its_descendant(dir_path) ⇒ Object
- .ensure_permanently_delete_file(file_path) ⇒ Object
- .file_list(dir = "*") ⇒ Object (also: files)
- .glob(query) ⇒ Object
- .headname(fname) ⇒ Object
- .help ⇒ Object
- .its_dir_base_ext(file_path) ⇒ Object
- .mainname(file_name) ⇒ Object
- .move_file(file_path, target_dir, mkdir: false, rename: false, rename_fmt: "__%C") ⇒ Object (also: movefile)
- .move_file_mkdir(file_path, target_dir) ⇒ Object (also: movefile_mkdir)
-
.rebasename_file(nodepath, new_basename) ⇒ Object
-[x] cannot move file or change its parent node | 重命名不改变源文件父节点,不移动源文件。.
-
.tailname(fname) ⇒ Object
< means headpoint of a basename, > means lastpoint of a basename.
- .tailnames(fname) ⇒ Object
-
.test_rebasename(path, new_basename) ⇒ Object
nut_a.black.you.one.
Class Method Details
.basename(file_name, suffix = "") ⇒ Object
72 73 74 |
# File 'lib/ezfile.rb', line 72 def basename file_name, suffix = "" File.basename file_name, suffix end |
.copy_file(file_path, dest_file_path) ⇒ Object Also known as: copyfile
125 126 127 128 129 130 |
# File 'lib/ezfile.rb', line 125 def copy_file file_path, dest_file_path if File.exist? dest_file_path return throw puts " FORBIDDEN ".light_white.on_yellow + " There is a same name node, cannot " + " COVER ".light_white.on_red.bold + " another existen node, Because will " + " LOSE ".light_white.on_red + " the FILE." end FileUtils.copy_file file_path, dest_file_path end |
.dir_list(dir = "*") ⇒ Object Also known as: dirs
140 141 142 |
# File 'lib/ezfile.rb', line 140 def dir_list dir = "*" Dir.glob(dir).select{File.directory? _1} end |
.ensure_permanently_delete_directory_and_its_descendant(dir_path) ⇒ Object
159 160 161 162 163 |
# File 'lib/ezfile.rb', line 159 def ensure_permanently_delete_directory_and_its_descendant dir_path if File.direcotry? dir_path FileUtils.remove_dir dir_path end end |
.ensure_permanently_delete_file(file_path) ⇒ Object
154 155 156 157 158 |
# File 'lib/ezfile.rb', line 154 def ensure_permanently_delete_file file_path if File.file? file_path FileUtils.remove_file file_path end end |
.file_list(dir = "*") ⇒ Object Also known as: files
136 137 138 |
# File 'lib/ezfile.rb', line 136 def file_list dir = "*" Dir.glob(dir).select{File.file? _1} end |
.glob(query) ⇒ Object
150 151 152 |
# File 'lib/ezfile.rb', line 150 def glob query Dir.glob(query) end |
.headname(fname) ⇒ Object
59 60 61 |
# File 'lib/ezfile.rb', line 59 def headname fname File.basename(fname)[0...-tailname(fname).size] end |
.help ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/ezfile.rb', line 165 def help puts " require \#{\"FileUtils\".red} and \#{\"Dir\".red}, \#{\"File\".red}.\n methods:\n \#{\" MOVE \".bold.underline.on_red}:\n \#{\"::move_file\".cyan} file_path target_dir, mkdir: false\n move file to target directory\n ! if directory is non-exist,\n throw an error.\n \#{\"::move_file \".cyan} file_path, target_dir\n if non-exist, mkdir\n \#{\" QUERY \".bold.underline.on_green}: its syntax like \#{\"Explorer Search\".underline}\n \#{\"::file_list \".cyan} target_dir = \"*\" alias \#{\"::files\".cyan}\n \#{\"::dir_list \".cyan} target_dir = \"*\" alias \#{\"::dirs\".cyan}\n show list of files/directories, of target_dir.\n ! using \#{\"Dir.glob(target_dir)\".italic.underline.yellow}\n \#{\"::its_dir_base_ext \".cyan} file_path\n return an array of file's dirpath basename extname.\n \#{\"::glob\".cyan} query\n the same as \#{\"Dir.glob(query)\".italic.underline.yellow}\n\n \#{\" RENAME \".bold.underline.on_yellow}:\n \#{\"::rebasename_file\".cyan} src_file_path, new_basename\n it change a file's basename, and ensure never move it or change its parent node.\n\n \#{\" DELETE \".bold.underline.on_red}:\n \#{\"::ensure_permanently_delete_file\".cyan} target_file alias \#{\"::files\".cyan}\n \#{\"::ensure_permanently_delete_directory_and_its_descendant\".cyan} target_dir alias \#{\"::files\".cyan}\n EOF\nend\n" |
.its_dir_base_ext(file_path) ⇒ Object
68 69 70 |
# File 'lib/ezfile.rb', line 68 def its_dir_base_ext file_path [File.dirname(file_path), File.basename(file_path, ".*"), File.extname(file_path)] end |
.mainname(file_name) ⇒ Object
63 64 65 |
# File 'lib/ezfile.rb', line 63 def mainname file_name File.basename file_name, ".*" end |
.move_file(file_path, target_dir, mkdir: false, rename: false, rename_fmt: "__%C") ⇒ Object Also known as: movefile
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ezfile.rb', line 76 def move_file file_path, target_dir, mkdir: false, rename: false, rename_fmt: "__%C" unless File.exist? file_path return throw puts " ERROR ".light_white.on_red + " File " + "non-exist".red end file_name = File.basename file_path # src_exist = File.exist? file_path # src_is_file = File.file? file_path rescue false # dest_exist = Dir.exist? target_dir # dest_is_dir = File.directory? target_dir rescue false # not_same_name = target_dir_entries.include? file_name rescue false isit_should_move = false if Dir.exist? target_dir target_dir_entries = Dir.entries target_dir if target_dir_entries.include? file_name if rename == true rename_count = 0 # nDigits = rename_fmt.count("#") begin rename_count += 1 # rename_postfix = rename_fmt.gsub(/#*/, "%0#{nDigits}d" % rename_count) src_dirpath, file_basename, file_ext = its_dir_base_ext(file_path) new_basename = file_basename + "__mv_rename_#{rename_count}" new_name = new_basename + file_ext new_name_file_path = File.join(src_dirpath, new_name) end while target_dir_entries.include? new_name rebasename_file(file_path, new_basename) return move_file(new_name_file_path, target_dir) end return throw puts " ERROR ".light_white.on_red + %Q( === Destination already has a same name node! -- "#{file_name.red}" ===) end isit_should_move = true elsif mkdir == true Dir.mkdir target_dir isit_should_move = true else return throw puts " ERROR ".light_white.on_red + " Destination Directory doesn't exist!" end if isit_should_move FileUtils.move file_path, target_dir end end |
.move_file_mkdir(file_path, target_dir) ⇒ Object Also known as: movefile_mkdir
132 133 134 |
# File 'lib/ezfile.rb', line 132 def move_file_mkdir file_path, target_dir self.move_file file_name, target_dir, mkdir: true end |
.rebasename_file(nodepath, new_basename) ⇒ Object
-[x] cannot move file or change its parent node | 重命名不改变源文件父节点,不移动源文件。
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ezfile.rb', line 27 def rebasename_file nodepath, new_basename if new_basename.split(/[\/\\]/).size == 0 return throw puts " wrong ".on_light_yellow + " That's no new basename" elsif new_basename.split(/[\/\\]/).size >= 2 return throw puts " FORBIDDEN ".light_white.on_yellow + " do not move its parent node, don't input a path" end src_dirpath = File.dirname File.(nodepath) unless File.exist? nodepath return throw puts " ERROR ".light_white.on_red + " File " + "non-exist".red end new_name = new_basename + File.extname(nodepath) unless Dir.entries(File.dirname nodepath).include? new_name File.rename nodepath, File.join(src_dirpath, new_name) end end |
.tailname(fname) ⇒ Object
< means headpoint of a basename, > means lastpoint of a basename. like ^ and $ in RegEx basename <name.dot1.dot2.ext> tailname .dot1.dot2.ext> # [A-Za-z_-.]*$ mainname <name .dot1.dot2 headname <name
52 53 54 |
# File 'lib/ezfile.rb', line 52 def tailname fname Ezfile.basename(fname).match(/((\.[0-9A-Za-z\-_]+)+$)/)[0] end |
.tailnames(fname) ⇒ Object
55 56 57 |
# File 'lib/ezfile.rb', line 55 def tailnames fname Ezfile.basename(fname).match(/((\.[0-9A-Za-z\-_]+)+$)/)[0].split(/(?=\.)/) end |
.test_rebasename(path, new_basename) ⇒ Object
nut_a.black.you.one
22 23 24 |
# File 'lib/ezfile.rb', line 22 def test_rebasename path, new_basename File.join(File.dirname(File.(path)), new_basename + File.extname(path)) end |