Skip to content

Instantly share code, notes, and snippets.

@rubyginner
Created November 25, 2012 14:49
Show Gist options
  • Save rubyginner/4143812 to your computer and use it in GitHub Desktop.
Save rubyginner/4143812 to your computer and use it in GitHub Desktop.
Organize your downloads
require 'fileutils'
path = '/Users/rubyginner/Downloads'
docs = ['.pdf', '.epub', '.txt', '.rtf']
pics = ['.png', '.gif', '.jpg', '.jpeg','.bmp', '.JPEG', '.JPG']
music = ['.mp3', '.wav']
videos = ['.mp4', '.wmv', '.flv', '.mov', '.mpeg']
apps = ['.dmg', '.pkg']
archive = ['.zip', '.tar']
Dir.foreach(path) { |dlfile|
next if ['.', '..'].include? dlfile
FileUtils.mv dlfile, path + '/Books' if docs.include? File.extname(dlfile)
FileUtils.mv dlfile, path + '/Pics' if pics.include? File.extname(dlfile)
FileUtils.mv dlfile, path + '/Music' if music.include? File.extname(dlfile)
FileUtils.mv dlfile, path + '/Videos' if videos.include? File.extname(dlfile)
FileUtils.mv dlfile, path + '/Apps' if apps.include? File.extname(dlfile)
FileUtils.mv dlfile, path + '/Archive' if archive.include? File.extname(dlfile)
}
@maricris-sn
Copy link

I haven't tried your script, but I find the following things you might want to try:

  • try extension.include? instead of the way you wrote it
  • try regular expression matching to get the extension just for practice
  • the way you composed your block is good for one liner actions; try using the do block

Anyhow, great work! Very creative! Keep it up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment