fileorganizer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ fileorganizer
2
+ =============
3
+
4
+ Renames files using the creation date and groups them into folders by date
5
+
6
+ WHAT IT DOES:
7
+
8
+ All files, including subfolders, are searched and organized into folders with naming convention of "YYYY-MM-DD". All files are renamed with the convention of "YYYY-MM-DD hour/minute/second -0800_1#####" where the last five digits increment if a duplicate filename. Any files that already have a name starting with the "YYYY-MM-DD" format will NOT be renamed, but will still be moved/organized into their corresponding folder.
9
+
10
+ HOW IT WORKS:
11
+
12
+ Install gem
13
+
14
+ CLI asks if you'd like to run the script starting from it's installed directory. If "n" (no) is selected then it will ask in which directory (full path) you'd like to search and organize files. It will then display all current files in this directory for review.
15
+
16
+ CLI then asks if you want to proceed with renaming the listed files and organizing them into folders. If "y" (yes) then the program will proceed. Once completed it will list out all the new files that have been renamed and the new folders that they have been moved into.
17
+
18
+ ISSUES:
19
+
20
+ When this gem is run against any file it will rename the file using the date value from the "File.ctime" method. Unfortunately after changing the filename of a file the "ctime" will then be updated to the time the filename was changed.
21
+
22
+ I have (so far) not been able to find a solution to this issue (at least for OSX) that keeps the "File.ctime" value in place even after renaming - including setting the "preserve" attribute when using the "File.rename" method (which simply blew up the rename method on OSX "Moutain Lion"). This unfortunately means that this code can not effectively be run against a file without the date associated with the file being changed.
23
+
24
+ A check has therefore been put in place so that the file will not be renamed if it's filename begins with the "YYYY-MM-DD" naming convention. Without this check a previously renamed file would simply get renamed again to the date that the previous rename took place.
25
+
26
+ Any ideas/solutions to the "File.ctime" issue when using the "File.rename" method would be much appreciated. Thx -B
27
+
28
+ RPSEC ISSUE:
29
+
30
+ Due to the same "copying files resets the File.ctime value" issue stated above, the Rspec "after" hook doesn't work properly as it copies the "testfiles_copy" image directory for use by the rpsec test. In doing so the File.ctime values of the "pre-organized" files are incorrect and will not match the values stated in the test. Therefore, after running each rspec test the "testfiles_copy" directory will need to be manually copied as "testfiles" in order to preserve the original File.ctime values. This is a pain in the butt (I know) but a necessecity until I find a solution to this issue. Sorry...
data/bin/fileorganizer ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
+ require 'fileorganizer'
data/lib/fileorganizer.rb CHANGED
@@ -1,48 +1,27 @@
1
- class FileOrganizer
2
-
3
- attr_accessor :directory_path, :file_type
4
-
5
- def initialize
6
- # get list of image files
7
- # rename image files based on creation date
8
- # create folders of for each date that where an image exists
9
- # move files into folder for that date
10
-
11
- # create tests
12
- # what does "full documentation" mean?
13
- # integrate with Travic CI
14
-
15
- # Dir.pwd
16
- end
17
-
18
- def directory_files
19
- files_sorted_by_time = Dir["#{directory_path}**/*.*"].sort_by{ |f| File.mtime(f) }
20
- end
21
-
22
- def rename_files
23
- directory_files.each do |filename|
24
- File.rename(filename, produce_name + id.to_s)
25
- id += 1
26
- end
27
- end
28
-
29
- def list_of_dates_from_files
30
- Dir["#{directory_path}**/*.jpg"]
31
- end
32
-
33
- def create_folders_from_file_dates
34
- # loop over unique dates and run "create_folder"
35
- Dir.mkdir(arg) unless Dir.exists?(arg)
36
- end
37
-
38
- def move_files_to_date_folders
39
- # loop over unique dates and run "create_folder"
1
+ require 'organize.rb'
2
+
3
+ newTask = Organize.new
4
+ puts "Do you want to run this script starting in your current directory (y/n)? (#{Dir.pwd}/)"
5
+ if gets.strip == 'y'
6
+ newTask.directory_path = Dir.pwd + '/'
7
+ else
8
+ puts "Enter the full directory path to the files you'd like to organize"
9
+ newTask.directory_path = gets.strip
10
+ end
11
+ puts newTask.directory_files
12
+ newTask.directory_files.each do |filename|
13
+ puts filename
14
+ end
15
+ puts "Rename & move files? (y/n)"
16
+ if gets.strip == 'y'
17
+ newTask.rename_files
18
+ newTask.move_files
19
+ newTask.directory_files.each do |filename|
20
+ puts filename
40
21
  end
41
-
22
+ puts "Done."
23
+ else
24
+ puts "Okay, come back later then."
42
25
  end
43
26
 
44
- # newTask = FileOrganizer.new
45
- # puts "Enter the full directory path to the files you'd like to organize"
46
- # newTask.directory_path = gets.strip
47
-
48
27
 
data/lib/organize.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'FileUtils'
2
+
3
+ class Organize
4
+
5
+ attr_accessor :directory_path
6
+
7
+ def initialize
8
+ # create tests
9
+ # what does "full documentation" mean?
10
+ # integrate with Travic CI
11
+ @directory_path = '/Users/brianward/Sites/filetest/'
12
+ end
13
+
14
+ def directory_files
15
+ files_sorted_by_time = Dir["#{directory_path}**/*.*"].sort_by{ |f| File.ctime(f) }
16
+ end
17
+
18
+ def rename_files
19
+ int = 100000
20
+ directory_files.each do |filename|
21
+ file_date = File.mtime(filename).to_s
22
+ if !/\d{4}\-\d{2}\-\d{2}/.match(filename[10])
23
+ if File.exist?(directory_path + file_date + filename[-4,4])
24
+ File.rename(filename, directory_path + file_date + '_' + int.to_s + filename[-4,4])
25
+ int += 1
26
+ else
27
+ File.rename(filename, directory_path + file_date + filename[-4,4])
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def move_files
34
+ # loop over the file dates and run "mkdir" for those dates that don't have a folder yet
35
+ directory_files.each do |filename|
36
+ folder_name = filename.split('/').last[0,10]
37
+ Dir.mkdir(directory_path + folder_name) unless Dir.exists?(directory_path + folder_name)
38
+ puts directory_path + folder_name
39
+ FileUtils.mv(filename, directory_path + folder_name)
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,37 @@
1
+ require './organize.rb'
2
+
3
+ describe Organize do
4
+
5
+ before :all do
6
+ @newTask = Organize.new
7
+ @newTask.directory_path = Dir.pwd + "/testfiles/"
8
+ end
9
+
10
+ after(:all) do
11
+ # FileUtils.rm_rf(@newTask.directory_path)
12
+ # current_directory = Dir.pwd
13
+ # FileUtils.cp_r(current_directory + '/testfiles_copy', current_directory + '/testfiles/')
14
+ end
15
+
16
+ it "should have a directory path" do
17
+ @newTask.should respond_to "directory_path"
18
+ end
19
+
20
+ it "should return a list of existing files in directory" do
21
+ existing_files = Dir[@newTask.directory_path + '**/*.*'].map { |a| File.basename(a) }
22
+ existing_files.should eq ["ben easy street.jpg", "mirror.jpg", "P1080679.jpg", "P1080689.jpg", "P1080693.jpg"]
23
+ end
24
+
25
+ it "should rename the existing files in directory" do
26
+ @newTask.rename_files
27
+ new_files = Dir[@newTask.directory_path + '**/*.*'].map { |a| File.basename(a) }
28
+ new_files.should eq ["2011-01-04 20:30:05 -0800.jpg", "2011-06-02 07:25:05 -0700.jpg", "2012-11-19 17:21:59 -0800.jpg", "2012-11-19 17:22:31 -0800.jpg", "2012-11-19 17:23:47 -0800.jpg"]
29
+ end
30
+
31
+ it "should create new folders from dates" do
32
+ @newTask.move_files
33
+ new_folders = Dir[@newTask.directory_path + '*/'].map { |a| File.basename(a) }
34
+ new_folders.should eq ["2011-01-04", "2011-06-02", "2012-11-19", "test"]
35
+ end
36
+
37
+ end
Binary file
Binary file
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fileorganizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,16 +9,61 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-30 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-12-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
14
46
  description: This is a gem that renames files based on their creation date and organizes
15
47
  them into folders for each date
16
48
  email: brianmatthewward@gmail.com
17
- executables: []
49
+ executables:
50
+ - fileorganizer
18
51
  extensions: []
19
52
  extra_rdoc_files: []
20
53
  files:
54
+ - bin/fileorganizer
21
55
  - lib/fileorganizer.rb
56
+ - lib/organize.rb
57
+ - lib/organize_spec.rb
58
+ - lib/testfiles_copy/4477744959_27ba8a402b_z.jpg
59
+ - lib/testfiles_copy/5241024543_4d024eaa09_b.jpg
60
+ - lib/testfiles_copy/5241024587_9ce803aafe_b.jpg
61
+ - lib/testfiles_copy/photo (1).JPG
62
+ - lib/testfiles_copy/photo (2).JPG
63
+ - lib/testfiles_copy/photo.JPG
64
+ - lib/testfiles_copy/test/0f8109ddc4c5d699bbf4d9fbef2bcfcb.jpg
65
+ - lib/testfiles_copy/test/3e54bfc519c423f3aa3b67d46d2684f4.jpg
66
+ - README.md
22
67
  homepage: http://rubygems.org/gems/fileorganizer
23
68
  licenses: []
24
69
  post_install_message: