Class: Lara::FileTools::Organizer

Inherits:
Object
  • Object
show all
Defined in:
lib/lara/file_tools/organizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir = ".") ⇒ Organizer

Returns a new instance of Organizer.



4
5
6
7
# File 'lib/lara/file_tools/organizer.rb', line 4

def initialize(dir = ".")
  @dir = dir
  @count = 0
end

Instance Method Details

#by_date_prefixObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lara/file_tools/organizer.rb', line 28

def by_date_prefix
  return "No directory: #{@dir}" unless Dir.exist?(@dir)
  results = []
  Dir.entries(@dir).sort.each do |f|
    next if f.start_with?(".") || !File.file?(File.join(@dir, f))
    @count += 1
    if @count > LIMIT
      results << "Trial limit reached (#{LIMIT} files). Full version: #{FileTools.full_version}"
      break
    end
    mtime = File.mtime(File.join(@dir, f))
    prefix = mtime.strftime("%Y-%m-%d")
    new_name = "#{prefix}_#{f}"
    File.rename(File.join(@dir, f), File.join(@dir, new_name))
    results << "#{f} -> #{new_name}"
  end
  results.empty? ? "No files to organize" : results.join("\n")
end

#by_extensionObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lara/file_tools/organizer.rb', line 9

def by_extension
  return "No directory: #{@dir}" unless Dir.exist?(@dir)
  results = []
  Dir.entries(@dir).sort.each do |f|
    next if f.start_with?(".") || !File.file?(File.join(@dir, f))
    @count += 1
    if @count > LIMIT
      results << "Trial limit reached (#{LIMIT} files). Full version: #{FileTools.full_version}"
      break
    end
    ext = File.extname(f).empty? ? "no_ext" : File.extname(f).delete(".")
    ext_dir = File.join(@dir, ext)
    Dir.mkdir(ext_dir) unless Dir.exist?(ext_dir)
    File.rename(File.join(@dir, f), File.join(ext_dir, f))
    results << "#{f} -> #{ext}/"
  end
  results.empty? ? "No files to organize" : results.join("\n")
end