Module: Fileman

Defined in:
lib/fileman/delete.rb,
lib/fileman/rename.rb,
lib/fileman/version.rb

Constant Summary collapse

VERSION =
"0.1.33724"

Class Method Summary collapse

Class Method Details

.remove(folder_path) ⇒ Object



7
8
9
10
11
12
# File 'lib/fileman/delete.rb', line 7

def remove(folder_path)
  new_path = Fileman.rename(folder_path, 'a', {:include_files => true, :ignore_ext => true, :recursive => true})
  FileUtils.rm_rf new_path
  # based on different environment, you may need to remove the folder a second time

  FileUtils.remove_dir new_path if File.exists? new_path
end

.rename(item_path, new_name, options = {}) ⇒ Object

Rename file or folder. If the item is a folder, subfolders and files can optionally be recursively renamed to.

Arguments:


item_path Item’s path new_name New name that is used to rename the item. If the item is a folder, subfolders and files can optionally be recursively renamed to. If there is multiple subfolders, then an incremetal number is appened straight after the ‘new_name’(e.g. if new_name is ‘a’, and there are 2 subfolder ‘subf_a’ and ‘subf_b’, then the new names will respectively be ‘a1’ and ‘a2’) options Optional parameters defined as follow: :recursive Default is false. If true, and if the item is a folder, then all the folder’s content is also renamed :include_files Default is false. If true, all files will also be renamed. In that case the ‘recursive’ option is implicitly set to true

+:ignore_ext+   Default is false. If true, and if 'include_files' is also true, then

file’s extensions are removed.

Returns the new name of the root folder



27
28
29
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fileman/rename.rb', line 27

def rename(item_path, new_name, options={})
  include_files = !options[:include_files].nil? && options[:include_files]
  recursive = include_files ? true : (!options[:recursive].nil? && options[:recursive])
  ignore_ext = !options[:ignore_ext].nil? && options[:ignore_ext]
  increment_name = lambda { |inc| inc == 0 ? new_name : "#{new_name}#{inc}" }

  try_rename_item = lambda { |counter, file, parent_folder, file_ext|
    final_name = nil
    begin
      while File.exists?(File.join(parent_folder, "#{increment_name.call(counter)}#{file_ext}")) do 
        counter+=1 
      end
      final_name = increment_name.call(counter)
      final_name = "#{final_name}#{file_ext}" unless ignore_ext
      FileUtils.mv file, File.join(parent_folder, final_name)
    rescue Errno::EACCES => eaccess # permission denied

      final_name = nil
    rescue Exception => e
      raise e
    end
    final_name
  }

  rename_item = lambda { |i|
    parent_folder = File.expand_path("../", i)
    file_ext = ignore_ext ? '' : File.extname(i)
    counter = 0
    final_name = nil
    while final_name == nil
      final_name = try_rename_item.call(counter, i, parent_folder, file_ext)
      counter += 1
    end
    File.join(parent_folder, final_name)
  }

  recursively_rename_item = lambda { |i|
    item_is_dir = File.directory? i
    new_item_path = rename_item.call(i) unless !item_is_dir && !include_files # rename current item


    if item_is_dir && recursive # if current is a folder, rename all its children

      all_folder_children = Dir.glob("#{new_item_path}/*", File::FNM_DOTMATCH) - ["#{new_item_path}/.", "#{new_item_path}/.."]
      all_folder_children.each { |c| recursively_rename_item.call(c) }
    end

    new_item_path
  }

  return recursively_rename_item.call(item_path)
end