fileorganizer 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -17
- data/lib/fileorganizer.rb +3 -0
- data/lib/organize.rb +18 -4
- data/lib/organize_spec.rb +12 -6
- data/lib/testfiles/4471473910_14e267969c.jpg +0 -0
- data/lib/testfiles/4474268581_2b1ebba1c1.jpg +0 -0
- data/lib/testfiles/4477744959_27ba8a402b_z.jpg +0 -0
- data/lib/testfiles/5241024543_4d024eaa09_b.jpg +0 -0
- data/lib/testfiles/5241024587_9ce803aafe_b.jpg +0 -0
- data/lib/testfiles/5241619844_a02287bb21_b.jpg +0 -0
- data/lib/testfiles/5241620170_91d55ef233_o.jpg +0 -0
- data/lib/testfiles/5384868249_80bcb10aa2_b.jpg +0 -0
- data/lib/testfiles/photo (1).JPG +0 -0
- data/lib/testfiles/photo (2).JPG +0 -0
- data/lib/testfiles/photo.JPG +0 -0
- data/lib/{testfiles_copy/test → testfiles/testfolder}/0f8109ddc4c5d699bbf4d9fbef2bcfcb.jpg +0 -0
- data/lib/{testfiles_copy/test → testfiles/testfolder}/3e54bfc519c423f3aa3b67d46d2684f4.jpg +0 -0
- data/lib/testfiles_copy/4471473910_14e267969c.jpg +0 -0
- data/lib/testfiles_copy/4474268581_2b1ebba1c1.jpg +0 -0
- data/lib/testfiles_copy/5241619844_a02287bb21_b.jpg +0 -0
- data/lib/testfiles_copy/5241620170_91d55ef233_o.jpg +0 -0
- data/lib/testfiles_copy/5384868249_80bcb10aa2_b.jpg +0 -0
- data/lib/testfiles_copy/testfolder/0f8109ddc4c5d699bbf4d9fbef2bcfcb.jpg +0 -0
- data/lib/testfiles_copy/testfolder/3e54bfc519c423f3aa3b67d46d2684f4.jpg +0 -0
- metadata +23 -5
data/README.md
CHANGED
@@ -1,30 +1,18 @@
|
|
1
1
|
fileorganizer
|
2
2
|
=============
|
3
3
|
|
4
|
-
Renames files using the
|
4
|
+
Renames files using the file modified date/time (File.mtime()) and groups them into folders by date
|
5
5
|
|
6
6
|
WHAT IT DOES:
|
7
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.
|
8
|
+
All files, including subfolders, are searched and organized into a single directory of folders with naming the 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. The previous folders that do not meet the "YYYY-MM-DD" naming convention will then be removed.
|
9
9
|
|
10
10
|
HOW IT WORKS:
|
11
11
|
|
12
12
|
Install gem
|
13
13
|
|
14
|
-
CLI
|
14
|
+
CLI "fileorganizer"
|
15
15
|
|
16
|
-
CLI then asks if you
|
16
|
+
CLI then 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.
|
17
17
|
|
18
|
-
|
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...
|
18
|
+
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.
|
data/lib/fileorganizer.rb
CHANGED
@@ -12,10 +12,13 @@ puts newTask.directory_files
|
|
12
12
|
newTask.directory_files.each do |filename|
|
13
13
|
puts filename
|
14
14
|
end
|
15
|
+
puts "Proposed file names:"
|
16
|
+
newTask.show_new_names
|
15
17
|
puts "Rename & move files? (y/n)"
|
16
18
|
if gets.strip == 'y'
|
17
19
|
newTask.rename_files
|
18
20
|
newTask.move_files
|
21
|
+
newTask.delete_non_date_folders
|
19
22
|
newTask.directory_files.each do |filename|
|
20
23
|
puts filename
|
21
24
|
end
|
data/lib/organize.rb
CHANGED
@@ -8,11 +8,17 @@ class Organize
|
|
8
8
|
# create tests
|
9
9
|
# what does "full documentation" mean?
|
10
10
|
# integrate with Travic CI
|
11
|
-
@directory_path = '/Users/brianward/Sites/
|
11
|
+
@directory_path = '/Users/brianward/Sites/fileorganizer/lib/testfiles/'
|
12
12
|
end
|
13
13
|
|
14
14
|
def directory_files
|
15
|
-
files_sorted_by_time = Dir["#{directory_path}**/*.*"].sort_by{ |f| File.
|
15
|
+
files_sorted_by_time = Dir["#{directory_path}**/*.*"].sort_by{ |f| File.mtime(f) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def show_new_names
|
19
|
+
directory_files.each do |filename|
|
20
|
+
puts directory_path + File.mtime(filename).to_s + filename[-4,4]
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
def rename_files
|
@@ -35,8 +41,16 @@ class Organize
|
|
35
41
|
directory_files.each do |filename|
|
36
42
|
folder_name = filename.split('/').last[0,10]
|
37
43
|
Dir.mkdir(directory_path + folder_name) unless Dir.exists?(directory_path + folder_name)
|
38
|
-
|
39
|
-
FileUtils.
|
44
|
+
FileUtils.copy_file(filename, directory_path + folder_name + "/" + filename.split('/').last, preserve = true)
|
45
|
+
FileUtils.remove_file(filename, force = true)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def delete_non_date_folders
|
50
|
+
Dir["#{directory_path}**/**"].each do |folder_name|
|
51
|
+
if !/\d{4}\-\d{2}\-\d{2}/.match(folder_name)
|
52
|
+
FileUtils.remove_dir(folder_name, force = false)
|
53
|
+
end
|
40
54
|
end
|
41
55
|
end
|
42
56
|
|
data/lib/organize_spec.rb
CHANGED
@@ -8,9 +8,9 @@ describe Organize do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
after(:all) do
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
FileUtils.rm_rf(@newTask.directory_path)
|
12
|
+
current_directory = Dir.pwd
|
13
|
+
FileUtils.cp_r current_directory + '/testfiles_copy', current_directory + '/testfiles/', :preserve => true
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should have a directory path" do
|
@@ -19,19 +19,25 @@ describe Organize do
|
|
19
19
|
|
20
20
|
it "should return a list of existing files in directory" do
|
21
21
|
existing_files = Dir[@newTask.directory_path + '**/*.*'].map { |a| File.basename(a) }
|
22
|
-
existing_files.should eq ["
|
22
|
+
existing_files.should eq ["4471473910_14e267969c.jpg", "4474268581_2b1ebba1c1.jpg", "4477744959_27ba8a402b_z.jpg", "5241024543_4d024eaa09_b.jpg", "5241024587_9ce803aafe_b.jpg", "5241619844_a02287bb21_b.jpg", "5241620170_91d55ef233_o.jpg", "5384868249_80bcb10aa2_b.jpg", "photo (1).JPG", "photo (2).JPG", "photo.JPG", "0f8109ddc4c5d699bbf4d9fbef2bcfcb.jpg", "3e54bfc519c423f3aa3b67d46d2684f4.jpg"]
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should rename the existing files in directory" do
|
26
26
|
@newTask.rename_files
|
27
27
|
new_files = Dir[@newTask.directory_path + '**/*.*'].map { |a| File.basename(a) }
|
28
|
-
new_files.should eq ["2011-
|
28
|
+
new_files.should eq ["2011-02-04 14:31:58 -0800.jpg", "2011-02-04 14:34:42 -0800.jpg", "2011-02-04 14:35:32 -0800.jpg", "2011-02-04 14:35:48 -0800.jpg", "2011-02-04 14:35:56 -0800.jpg", "2011-02-04 14:37:02 -0800.jpg", "2011-02-04 14:37:26 -0800.jpg", "2011-02-04 14:37:58 -0800.jpg", "2011-02-21 10:55:15 -0800.jpg", "2011-02-21 10:56:09 -0800.jpg", "2011-03-19 22:24:33 -0700.JPG", "2011-03-19 22:24:45 -0700.JPG", "2011-03-19 22:24:56 -0700.JPG"]
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should create new folders from dates" do
|
32
32
|
@newTask.move_files
|
33
33
|
new_folders = Dir[@newTask.directory_path + '*/'].map { |a| File.basename(a) }
|
34
|
-
new_folders.should eq ["2011-
|
34
|
+
new_folders.should eq ["2011-02-04", "2011-02-21", "2011-03-19", "testfolder"]
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should delete old folders that are now dates" do
|
38
|
+
@newTask.delete_non_date_folders
|
39
|
+
new_folders = Dir[@newTask.directory_path + '*/'].map { |a| File.basename(a) }
|
40
|
+
new_folders.should eq ["2011-02-04", "2011-02-21", "2011-03-19"]
|
35
41
|
end
|
36
42
|
|
37
43
|
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
File without changes
|
File without changes
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
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.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
-
description: This is a gem that renames files based on their
|
46
|
+
description: This is a gem that renames files based on their 'modified date' and organizes
|
47
47
|
them into folders for each date
|
48
48
|
email: brianmatthewward@gmail.com
|
49
49
|
executables:
|
@@ -55,16 +55,34 @@ files:
|
|
55
55
|
- lib/fileorganizer.rb
|
56
56
|
- lib/organize.rb
|
57
57
|
- lib/organize_spec.rb
|
58
|
+
- lib/testfiles/4471473910_14e267969c.jpg
|
59
|
+
- lib/testfiles/4474268581_2b1ebba1c1.jpg
|
60
|
+
- lib/testfiles/4477744959_27ba8a402b_z.jpg
|
61
|
+
- lib/testfiles/5241024543_4d024eaa09_b.jpg
|
62
|
+
- lib/testfiles/5241024587_9ce803aafe_b.jpg
|
63
|
+
- lib/testfiles/5241619844_a02287bb21_b.jpg
|
64
|
+
- lib/testfiles/5241620170_91d55ef233_o.jpg
|
65
|
+
- lib/testfiles/5384868249_80bcb10aa2_b.jpg
|
66
|
+
- lib/testfiles/photo (1).JPG
|
67
|
+
- lib/testfiles/photo (2).JPG
|
68
|
+
- lib/testfiles/photo.JPG
|
69
|
+
- lib/testfiles/testfolder/0f8109ddc4c5d699bbf4d9fbef2bcfcb.jpg
|
70
|
+
- lib/testfiles/testfolder/3e54bfc519c423f3aa3b67d46d2684f4.jpg
|
71
|
+
- lib/testfiles_copy/4471473910_14e267969c.jpg
|
72
|
+
- lib/testfiles_copy/4474268581_2b1ebba1c1.jpg
|
58
73
|
- lib/testfiles_copy/4477744959_27ba8a402b_z.jpg
|
59
74
|
- lib/testfiles_copy/5241024543_4d024eaa09_b.jpg
|
60
75
|
- lib/testfiles_copy/5241024587_9ce803aafe_b.jpg
|
76
|
+
- lib/testfiles_copy/5241619844_a02287bb21_b.jpg
|
77
|
+
- lib/testfiles_copy/5241620170_91d55ef233_o.jpg
|
78
|
+
- lib/testfiles_copy/5384868249_80bcb10aa2_b.jpg
|
61
79
|
- lib/testfiles_copy/photo (1).JPG
|
62
80
|
- lib/testfiles_copy/photo (2).JPG
|
63
81
|
- lib/testfiles_copy/photo.JPG
|
64
|
-
- lib/testfiles_copy/
|
65
|
-
- lib/testfiles_copy/
|
82
|
+
- lib/testfiles_copy/testfolder/0f8109ddc4c5d699bbf4d9fbef2bcfcb.jpg
|
83
|
+
- lib/testfiles_copy/testfolder/3e54bfc519c423f3aa3b67d46d2684f4.jpg
|
66
84
|
- README.md
|
67
|
-
homepage:
|
85
|
+
homepage: https://github.com/brianward/fileorganizer
|
68
86
|
licenses: []
|
69
87
|
post_install_message:
|
70
88
|
rdoc_options: []
|