Skip to content

Instantly share code, notes, and snippets.

@jamiew
Forked from luke0x/gist:136569
Created July 1, 2009 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiew/138997 to your computer and use it in GitHub Desktop.
Save jamiew/138997 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# sup luke; jdubs here
# - put the docs and helper scripts inline for syntax highlighting joy
# - added another crontab example -- I like to file once/day.
# plays nice with treating the Desktop like a daily scratchpad
# - added a 'Yesterday' link in addition to Today
# - minor tweaks around town
## config
HOME_DIRECTORY = "~" # this will
DESKTOP_DIRECTORY = "#{HOME_DIRECTORY}/Desktop"
STORAGE_DIRECTORY = "#{HOME_DIRECTORY}/Documents"
LOG_DIRECTORY = "#{HOME_DIRECTORY}/.bin"
SHELL = "/bin/bash"
## docs
=begin
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 your path)
4. `chmod +x` the 3 files
5. `crontab -e` this (as your user):
# drefile desktop every 5 minutes (on the 5)
0,5,10,15,20,25,30,35,40,45,50,55 * * * * ~/.bin/drefile >> /Users/luke/.bin/drefile_cron.log
# drefile desktop once/day (at 5am)
* 5 * * * ~/.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.rb
-------
=end
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")+' '
timestamp_prefix = '' #i don't like the prefixes, just the directories
# 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