Class: OrganizeFiles::FileHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/organize_files/file_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ FileHandler

Returns a new instance of FileHandler.



7
8
9
# File 'lib/organize_files/file_handler.rb', line 7

def initialize(directory)
  @directory = directory
end

Instance Method Details

#move_file(file, category) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/organize_files/file_handler.rb', line 15

def move_file(file, category)
  source_path = File.join(@directory, file)
  destination = File.join(@directory, category)

  FileUtils.mkdir_p(destination) unless File.directory?(destination)

  destination_path = File.join(destination, File.basename(source_path))

  if File.exist?(source_path)
    FileUtils.mv(source_path, destination_path)
    puts "Successfully moved #{File.basename(source_path)} to #{destination_path}"
  else
    puts "File not found: #{source_path}"
  end
end

#remove_empty_foldersObject



31
32
33
34
35
36
37
# File 'lib/organize_files/file_handler.rb', line 31

def remove_empty_folders
  Dir.entries(@directory).each do |file|
    path = File.join(@directory, file)

    Dir.rmdir(path) if Dir.empty?(path)
  end
end

#scan_filesObject



11
12
13
# File 'lib/organize_files/file_handler.rb', line 11

def scan_files
  Dir.new(@directory).children
end