Class: Organize

Inherits:
Object
  • Object
show all
Defined in:
lib/organize.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOrganize

Returns a new instance of Organize.

[View source]

7
8
9
10
11
12
# File 'lib/organize.rb', line 7

def initialize
	# create tests
	# what does "full documentation" mean?
	# integrate with Travic CI 
	@directory_path = '/Users/brianward/Sites/fileorganizer/lib/testfiles/'
end

Instance Attribute Details

#directory_pathObject

Returns the value of attribute directory_path.


5
6
7
# File 'lib/organize.rb', line 5

def directory_path
  @directory_path
end

Instance Method Details

#delete_non_date_foldersObject

[View source]

49
50
51
52
53
54
55
# File 'lib/organize.rb', line 49

def delete_non_date_folders
	Dir["#{directory_path}**/**"].each do |folder_name|
		if !/\d{4}\-\d{2}\-\d{2}/.match(folder_name)
			FileUtils.remove_dir(folder_name, force = false)
		end
	end
end

#directory_filesObject

[View source]

14
15
16
# File 'lib/organize.rb', line 14

def directory_files
	files_sorted_by_time = Dir["#{directory_path}**/*.*"].sort_by{ |f| File.mtime(f) }
end

#move_filesObject

[View source]

39
40
41
42
43
44
45
46
47
# File 'lib/organize.rb', line 39

def move_files
	# loop over the file dates and run "mkdir" for those dates that don't have a folder yet
	directory_files.each do |filename|
		folder_name = filename.split('/').last[0,10]
		Dir.mkdir(directory_path + folder_name) unless Dir.exists?(directory_path + folder_name)
		FileUtils.copy_file(filename, directory_path + folder_name + "/" + filename.split('/').last, preserve = true)
		FileUtils.remove_file(filename, force = true)
	end
end

#rename_filesObject

[View source]

24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/organize.rb', line 24

def rename_files
	int = 100000
	directory_files.each do |filename|
		file_date = File.mtime(filename).to_s
		if !/\d{4}\-\d{2}\-\d{2}/.match(filename[10])
			if File.exist?(directory_path + file_date + filename[-4,4])
		    	File.rename(filename, directory_path + file_date + '_' + int.to_s + filename[-4,4])
		    	int += 1
		    else
		    	File.rename(filename, directory_path + file_date + filename[-4,4])
		    end
		end
	end
end

#show_new_namesObject

[View source]

18
19
20
21
22
# File 'lib/organize.rb', line 18

def show_new_names
	directory_files.each do |filename|
		puts directory_path + File.mtime(filename).to_s + filename[-4,4]
	end
end