Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Organize photos and other files on an year/month/day folders structures

This script can organize you media colelction in folders by year/month/day.
For jpeg files, looks in exif for the creation date, if that file has that kind of metadadata. For any other files it only checks the creation time.
The script takes 3 option on the command line. A command which may be -m, -c or -f, a source folder and a destination folder.
Using -c will copy the files from souce to destination, -m will move them, - f will also move them, by making first a copy then a delete. -f option may used when -m doesn't work, the most common situation beeing when you want to move the files from one file system to another(e.g. from your ext3 local hard drive to an external fat32 usb harddrive)
After you organize the files, you can find duplicate items using my previous snippet.
This script have been inspired from here
   1  
   2  #!/usr/local/bin/ruby
   3  require 'rubygems'
   4  require 'ftools'
   5  require 'exifr'
   6  
   7  
   8  def process_file(file_path,destination_dir)
   9  	if File.directory?(file_path)
  10  		crt_dir = Dir.new(file_path)
  11  		crt_dir.each do |file_name|
  12  			if file_name != '.' &&  file_name != '..'				
  13  				process_file("#{crt_dir.path}/#{file_name}",destination_dir)		
  14  			end
  15  		end	
  16  	else
  17  			
  18  		if  File.fnmatch('*.jpg',file_path) ||  File.fnmatch('*.jpeg',file_path)
  19  			picture = EXIFR::JPEG.new(file_path)
  20  			if picture != nil && picture.exif != nil
  21  				file_date = picture.date_time		
  22  			else
  23  				f = File.new(file_path)
  24  				file_date = f.mtime
  25  			end							
  26  		end
  27  		if file_date == nil
  28  			f = File.new(file_path)
  29  			file_date = f.mtime	
  30  		end		
  31  		year_dir =  destination_dir + file_date.strftime("%Y")
  32  		month_dir = destination_dir + file_date.strftime("%Y/%m-%b")
  33  		day_dir = destination_dir + file_date.strftime("%Y/%m-%b/%d")
  34  		new_file_name = day_dir + "/" + File.basename(file_path)
  35  		begin
  36  			Dir.mkdir(year_dir) unless File.exists?(year_dir)
  37  			Dir.mkdir(month_dir) unless File.exists?(month_dir)
  38  			Dir.mkdir(day_dir) unless File.exists?(day_dir)
  39  			if ARGV[0 ] =='-m' #move the files
  40  				File.rename(file_path, new_file_name)
  41  			elsif ARGV[0]  =='-c' #copy the files
  42  				File.cp(file_path, new_file_name)	
  43  			elsif ARGV[0]  =='-f' #copy and delete, acts like a move between thw different file systems	
  44  				File.cp(file_path, new_file_name)
  45  				File.delete(file_path)	
  46  			else
  47  				puts "Unknown option #{ARGV[0]}"
  48  				exit	
  49  			end	
  50  		end	
  51  		
  52  	end	
  53  end
  54  
  55  
  56  if ARGV.length != 3
  57  	puts "Three arguments are required to run the script, -c|-m|-f <source_folder_or_file>  <destination_folder>"
  58  	exit
  59  end
  60  
  61  if ARGV[0] !='-c'  && ARGV[0]!='-m' && ARGV[0]!='-f'
  62  	puts "Unknown running option: #{ARGV[0]}"
  63  	exit
  64  end
  65  
  66  if not File.exists?(ARGV[1]) 
  67  	puts "Source file does not exists: #{ARGV[1]}"
  68  	exit
  69  end
  70  
  71  
  72  if not File.directory?(ARGV[2]) 
  73  	puts "Destination file is not a directory #{ARGV[2]}"
  74  	exit
  75  end
  76  
  77  if ARGV[1]==ARGV[2]
  78  	puts "Source and destination must be different"
  79  	exit
  80  end	
  81  	
  82  	
  83  process_file(ARGV[1], ARGV[2])
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS