├── test_files.rb
├── README.md
└── file_organizer.rb
/test_files.rb:
--------------------------------------------------------------------------------
1 | file_types=["doc","pdf","mp3","txt","mp4","cpp","in","out"]
2 | i=0
3 | rnd=Random.new
4 | file_types.each do |type|
5 | rnd.rand(5..15).times do
6 | f=File.new("test_file#{i}."+"#{type}","w")
7 | f.close
8 | i=i+1
9 | end
10 | end
11 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # file_organizer
2 | A basic hack to organize files in a folder. Categorizes different files in corresponding folders.
3 | Currently very basic utility.
4 | Working: 
5 | How to run:
6 | Add the path of the directory(file_organizer) to PATH variable in linux.
7 | Change the directory to one which you want to organize
8 | Then do ruby file_organizer.rb to run
9 | Or to make it executable do chmod +x file_organizer.rb
10 | Then you can run as ./file_organizer.rb
11 |
12 | Follow this to make it a executable script: http://commandercoriander.net/blog/2013/02/16/making-a-ruby-script-executable/
13 | Then you can simple run with typing file_organizer
14 |
15 | Choose the option and see the results.
16 | Two options:
17 | 1. Press 1: Manually enter file extensions
18 | eg: pdf,doc,mp3
19 | The list should be comma seperated.
20 | 2. Press 2: Organize everything.
21 |
22 | Requirements:
23 | Ruby 2.1
24 |
25 | To do:
26 | 1. Command line interface
27 | 2. Addition of more type of files
28 | 3. Currently not handling any exceptions.
29 |
--------------------------------------------------------------------------------
/file_organizer.rb:
--------------------------------------------------------------------------------
1 | #!/usr/bin/ruby
2 | require 'fileutils'
3 |
4 |
5 | #You can add more file types. If you have not mentioned a certain file type, a new folder will be created for it.
6 | docs=["DOC","DOCX","PPT","PPTX","PAGES","PDF","ODT","ODP","XLSX","XLS","ODS","TXT","IN","OUT"]
7 | images=["JPG","JPEG","GIF","PNG","SVG"]
8 | audio=["MP3","WAV","WMA","MKA","AAC","MID","RA","RAM","RM","OGG"]
9 | video=["FLV","WMV","MOV","MP4","MPEG","3GP","MKV"]
10 | code=["CPP","RB","PY","HTML","CSS","JS"]
11 | sys_files=["DEB","EXE","SH","BUNDLE"]
12 | compressed=["RAR","JAR","ZIP","TAR","MAR","ISO","LZ","7ZIP","TGZ","GZ","BZ2"]
13 |
14 | #--------------------------------------------------methods-------------------------------------------#
15 | def create_directories(file_types)
16 | hash_files=Hash.new
17 | file_types.each do |type|
18 | if !Dir.exists?("#{type.upcase} files")
19 | Dir.mkdir("#{type.upcase} files")
20 | end
21 | hash_files[type]=0
22 | end
23 | return hash_files
24 | end
25 |
26 | def organize(hash_files,selected,file_formats) #Use of selected: If selected is true that means the option selected in beginning was 1.
27 | #Only the user selected filetypes will be organized other wise all files will be organized
28 |
29 | #Analysis is used to count each type of file,directories and unknown files.
30 | analysis=Hash.new
31 | analysis["directory"]=0
32 | analysis["unknown"]=0
33 |
34 | #Get the current directory object
35 | present_dir=Dir.pwd
36 | dir=Dir.new(present_dir)
37 |
38 | dir.each do |file|
39 | if(file!="file_organizer.rb") #Do not put the file_organizer.rb in folder.
40 |
41 | if Dir.exist?(file) #Check whether the file is Directory
42 |
43 | analysis["directory"]=analysis["directory"] + 1
44 |
45 | elsif File.file?(file) #Whether it is a general file type or not.
46 |
47 | ext_name=File.extname(file)
48 | ext_name=ext_name.split(".")[1].to_s
49 | #puts "extensions: " + ext_name
50 |
51 | #---------------------Analysis-------------------------------#
52 | if analysis.has_key?(ext_name)
53 | analysis[ext_name]= analysis[ext_name] + 1
54 | else
55 | analysis[ext_name]=1
56 | end
57 | #---------------------------------------------------------#
58 |
59 | if selected
60 |
61 | if hash_files.has_key?(ext_name)
62 | hash_files[ext_name]=hash_files[ext_name]+1
63 | source=File.absolute_path(file)
64 | FileUtils.mv(present_dir+"/"+file, present_dir + "/#{ext_name.upcase} files/"+file)
65 | end
66 |
67 | else
68 |
69 | if file_formats.has_key?(ext_name.upcase)
70 | if !Dir.exists?(file_formats[ext_name.upcase])
71 | Dir.mkdir(file_formats[ext_name.upcase])
72 | end
73 | source=File.absolute_path(file)
74 | FileUtils.mv(present_dir+"/"+file, present_dir + "/#{file_formats[ext_name.upcase]}/"+file)
75 | else
76 | if !Dir.exists?("#{ext_name.upcase} files")
77 | Dir.mkdir("#{ext_name.upcase} files")
78 | end
79 | source=File.absolute_path(file)
80 | FileUtils.mv(present_dir+"/"+file, present_dir + "/#{ext_name.upcase} files/"+file)
81 | end
82 |
83 | end
84 |
85 | else
86 |
87 | analysis["unknown"]=analysis["unknown"]+1
88 | end
89 | end
90 | end
91 |
92 | return analysis
93 |
94 | end
95 |
96 | #--------------------------------------------------end of methods-------------------------------------------#
97 |
98 |
99 | #----------------------------------------------Main Program--------------------------------------------------------#
100 | puts "Hi,thanks for using file_organizer!"
101 | puts "Press 1: Manually enter file extensions"
102 | puts "Press 2: Organize everything"
103 | i=gets.chomp().to_i
104 | file_formats=Hash.new
105 |
106 | #------------------------------Making a hash for different type of files---------------------------------#
107 | #To add your own type of files, First add type and its corresponding extensions on top. Eg: audio=["MP3","WMA"]. Extension should be in capital.
108 | #Then add a for loop here adding the extensions to the hash (file_formats)
109 |
110 | docs.each do |x|
111 | file_formats[x]="DOC Files"
112 | end
113 |
114 | audio.each do |x|
115 | file_formats[x]="Audio Files"
116 | end
117 |
118 | video.each do |x|
119 | file_formats[x]="Video Files"
120 | end
121 |
122 | images.each do |x|
123 | file_formats[x]="Image Files"
124 | end
125 |
126 | code.each do |x|
127 | file_formats[x]="Code Files"
128 | end
129 |
130 | sys_files.each do |x|
131 | file_formats[x]="SYS Files"
132 | end
133 |
134 | compressed.each do |x|
135 | file_formats[x]="Compressed files"
136 | end
137 |
138 | #----------------------------------------------end of hash map----------------------------------------/
139 |
140 | hash_files=Hash.new
141 |
142 | if(i==1)
143 | puts "Enter file extensions you need to organize(seperated by a comma eg. \"doc,pdf,mp3\"):"
144 | input=gets.chomp()
145 | file_types=input.split(",")
146 | hash_files=create_directories(file_types)
147 | analysis=organize(hash_files,true,file_formats)
148 | else
149 | puts "Just sit back and relax!"
150 | analysis=organize(hash_files,false,file_formats)
151 | end
152 |
153 | puts "Your directory contains: "
154 | total=0
155 | analysis.each do |key,value|
156 | total=total+value
157 | puts "#{key.upcase} files: #{value}"
158 | end
159 | puts "Total files: #{total}"
160 | #--------------------------------------------------End----------------------------------------------------#
161 |
--------------------------------------------------------------------------------