Less than 2 sec to Clean and Organise your Desktop and Downloads
Hi, is your Desktop look messy with unwanted files or your downloads folder is filled with too many items. You can clean it in Less than 5 sec.
We are going to use a simple Ruby script that automatically categories the files and moves them to the current folder.
Github link: https://github.com/aravindaakash/selenium-automation/tree/main/desktop_cleaner
Source Code: (On-demand clean up)
require 'fileutils' home_path = Dir.home source_location = "#{home_path}/Desktop" destination_location = "#{home_path}/Documents" destination_folder = "#{destination_location}/Desktop_files" source_files = Dir.glob("#{source_location}/*") Dir.mkdir(destination_folder) unless Dir.exist?(destination_folder) errors_messages = [] source_files.each do |file| destination_type_folder = destination_folder unless File.directory?(file) file_type = file.split('.').last destination_type_folder = "#{destination_folder}/#{file_type}" Dir.mkdir(destination_type_folder) unless Dir.exist?(destination_type_folder) end begin FileUtils.mv(file, destination_type_folder) rescue errors_messages << file end end puts "Successfully cleared #{source_files.count-errors_messages.count} files" puts "Failed to clear file(s) due to \n #{errors_messages.join("\n")}" if errors_messages.any?
In the above script, I moved my files from the source Desktop to destination Documents
The script will create a folder in the name of file extensions and move the files to the current folder so that we can group and search the files easily
Sample video
We can also modify the script to organize the Downloads folder like to group the downloaded items based on the file types so that it just looks fantastic and pretty organized.
You can use this script on real-time cleaning purposes too. Like whenever a file is added in desktop it will automatically move the file to the corresponding folder.
Source Code: (Scheduled clean up)
require 'fileutils' home_path = Dir.home source_location = "#{home_path}/Desktop" destination_location = "#{home_path}/Documents" destination_folder = "#{destination_location}/Desktop_files" Dir.mkdir(destination_folder) unless Dir.exist?(destination_folder) #infinite loop while true errors_messages = [] source_files = Dir.glob("#{source_location}/*") source_files.each do |file| destination_type_folder = destination_folder unless File.directory?(file) file_type = file.split('.').last destination_type_folder = "#{destination_folder}/#{file_type}" Dir.mkdir(destination_type_folder) unless Dir.exist?(destination_type_folder) end begin FileUtils.mv(file, destination_type_folder) rescue errors_messages << file end end puts "Successfully Cleared #{source_files.count-errors_messages.count} files" puts "Failed to Cleared file(s) due to \n #{errors_messages.join("\n")}" if errors_messages.any? sleep 30 # clean up will run twices for every min you can change the frequency end
Comments
Post a Comment