Sha256: 4d9cc2d481a270bcea9984cc9877ce8ce0b13a4ef1e3a40de10af0a20d925a3f

Contents?: true

Size: 949 Bytes

Versions: 1

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

require "fileutils"

module OrganizeFiles
  class FileHandler
    def initialize(directory)
      @directory = directory
    end

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

    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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
organize_files-1.1.0 lib/organize_files/file_handler.rb