DEV Community

Cover image for Organize Your Downloads Folder with Ruby
Mattias Velamsson
Mattias Velamsson

Posted on

1

Organize Your Downloads Folder with Ruby

Are you like me and have an ocean of files in your downloads folder? No matter how I try or promise myself I'll clean it up daily I never do. So, this morning I decided to throw a small app together with Ruby and do something about it!


What the app does:

  • Goes through all the files and directories in your Downloads folder and checks the last modified date, and then moves the file/directory into a subfolder in downloads according to the year it was last modified.
require 'find'
require 'fileutils'


downloads_folder = File.join(Dir.home, "Downloads")

## Array of years, used in the filtering logic.
yearly_folders = ["2019", "2020", "2021", "2022", "2023", "2024", "2025"]

Dir.foreach(downloads_folder) do |file|
## Filtering logic.
## Skip Subfolders and Foldersnames included in "yearly_folders" array.
  next if file == "." || file == ".." || yearly_folders.include?(file)

  ## Makes creates a string for the path of the file/directory
  path = File.join(downloads_folder, file)
  next unless File.exists?(path)

  ## Getting the modified date of the file/directory.
  last_edited = File.mtime(path)
  year = last_edited.strftime("%Y")
  new_directory = ("#{downloads_folder}/#{year}")

  ## If a folder with modified file's year does not exist - create it.
  unless File.directory?(new_directory)
    puts "Create Folder"
    Dir.mkdir(new_directory)
  end

  ## If the file/directory is NOT the same as the new_directory, move it to the new directory.
  if path != new_directory
    FileUtils.mv(path,new_directory)
  end
end

Enter fullscreen mode Exit fullscreen mode

RESULT

result gif

Now, this works well enough for me right now. I've also added it to automatically run whenever I login to my account. 💪🏻

But in the future I'll be implementing:

  • Subfolders for months
  • Filter by filetype

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay