Skip to content

Instantly share code, notes, and snippets.

@luke0x
Created June 26, 2009 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save luke0x/136569 to your computer and use it in GitHub Desktop.
Save luke0x/136569 to your computer and use it in GitHub Desktop.
drefile: an automatic, chronological file organizer
---------------------------------------------------
Every few minutes drefile copies all files on your Desktop to a folder
such as ~/Documents/2009-06-June/26-Friday, prepending a timestamp
(like 20090626.1310) to the filename.
Use
---
1. Put files on your Desktop, and in a few minutes they will be 'drefiled'
2. Use the 'Today' alias on your Desktop to go to today's folder
3. Type 'dday' to go to today's directory
4. Type 'dmonth' to go to this month's directory
5. Type 'drefile file' to manually timestamp + move a file
Install
-------
1. sudo port install osxutils
2. sudo gem install open4
3. copy the 3 files below to ~/.bin (or whatever, in the path)
4. chmod +x the 3 files
5. crontab -e this: 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /Users/luke/.bin/drefile >>
/Users/luke/.bin/drefile_cron.log
dday.rb
-------
#!/usr/bin/env ruby
require "fileutils"
month_and_day = Time.now.strftime("%Y-%m-%B/%d-%A")
dir = "/Users/luke/Documents/#{month_and_day}"
FileUtils.mkdir_p dir
puts "cd #{dir}"
dmonth.rb
---------
#!/usr/bin/env ruby
require "fileutils"
month = Time.now.strftime("%Y-%m-%B")
dir = "/Users/luke/Documents/#{month}"
FileUtils.mkdir_p dir
puts "cd #{dir}"
drefile
-------
#!/usr/bin/env ruby
# desktop refile
# requires mkalias from the osxutils port
# cron to run every 5 minutes
# 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /Users/luke/.bin/drefile.rb >> /Users/luke/.bin/drefile_cron.log
HOME_DIRECTORY = "/Users/luke"
DESKTOP_DIRECTORY = "#{HOME_DIRECTORY}/Desktop"
STORAGE_DIRECTORY = "#{HOME_DIRECTORY}/Documents"
LOG_DIRECTORY = "#{HOME_DIRECTORY}/.bin"
SHELL = "/bin/bash"
require 'rubygems'
require 'open4'
require 'logger'
require "fileutils"
require 'time'
if ARGV.length == 0
# make the drefile dir
month_and_day = Time.now.strftime("%Y-%m-%B/%d-%A")
drefile_directory = "#{STORAGE_DIRECTORY}/#{month_and_day}"
FileUtils.mkdir_p(drefile_directory)
timestamp_prefix = Time.now.strftime("%Y%m%d.%H%M")
# remove the today alias
FileUtils.rm("#{DESKTOP_DIRECTORY}/Today") if File.exists?("#{DESKTOP_DIRECTORY}/Today")
# move files with a datetime prefix
Dir.entries(DESKTOP_DIRECTORY).reject{|f| f =~ /^\./}.each do |file_name|
FileUtils.mv("#{DESKTOP_DIRECTORY}/#{file_name}", "#{drefile_directory}/#{timestamp_prefix} #{file_name}")
end
# remake the today alias
`/opt/local/bin/mkalias -r "#{drefile_directory}" "#{HOME_DIRECTORY}/Desktop/Today"`
else
# make the drefile dir
timestamp = Time.parse(Open4.open4("mdls -name kMDItemFSCreationDate -raw #{ARGV.first}")[2].read)
drefile_directory = "#{STORAGE_DIRECTORY}/#{timestamp.strftime("%Y-%m-%B/%d-%A")}"
FileUtils.mkdir_p(drefile_directory)
new_file = "#{drefile_directory}/#{timestamp.strftime("%Y%m%d.%H%M")} #{ARGV.first}"
FileUtils.mv(ARGV.first, new_file)
puts new_file
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment