file_sorter_tool 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Rakefile +23 -0
- data/bin/file_sorter_tool +11 -0
- data/lib/file_sorter_tool/file_sorter.rb +65 -0
- data/lib/file_sorter_tool.rb +22 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 617bc3901c6d9e2a848480ac42221ec2cece2988
|
4
|
+
data.tar.gz: 43d3a33a8c9b2d833d5986aac08ca58b91b82bc8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 48a3ce2533eec334a9f9996afa918d9e73bf8659d40d8e158b8e7dfe56da7816a942c9704437a2fd8c257c6c0fb6100c27b8d6286cb87bc697f31889d2b1360f
|
7
|
+
data.tar.gz: 341d9845460eb9d8d9a3521a0c03f8d660c0fb54ad678d0eda4043b4ee59518a377a3a0e1760e04608fcc0381b4dabe487807adfac3545e7c7159e8a57b57af9
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'file_sorter_tool'
|
2
|
+
|
3
|
+
task default: 'sort:help'
|
4
|
+
|
5
|
+
namespace :sort do
|
6
|
+
|
7
|
+
desc 'help'
|
8
|
+
task :help do
|
9
|
+
puts "v. #{FileSorterTool::VERSION}.\nFiles in current directory sorter.\nTool makes directory for each file extenfion and move files into this directories."
|
10
|
+
end
|
11
|
+
|
12
|
+
desc 'sort files'
|
13
|
+
task :do, [:path] do |t, args|
|
14
|
+
path = args[:path]
|
15
|
+
if path == nil || path.empty?
|
16
|
+
puts "path is empty!"
|
17
|
+
else
|
18
|
+
Dir.chdir(path)
|
19
|
+
FileSorterTool::process_dir path
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'active_support/core_ext/string'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module FileSorterTool
|
5
|
+
|
6
|
+
class FileSorter
|
7
|
+
|
8
|
+
def process
|
9
|
+
return if File.directory?(@from_path)
|
10
|
+
to_file_path = "#{@to_dir_path}/#{@from_file}"
|
11
|
+
create_directory
|
12
|
+
|
13
|
+
new_file_name = get_new_file_name(to_file_path, @from_file)
|
14
|
+
rename_and_move_files(new_file_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def initialize file_name
|
20
|
+
@from_file = File.basename(file_name)
|
21
|
+
@from_path = File.absolute_path(file_name)
|
22
|
+
|
23
|
+
ext_name = File.extname(@from_file)
|
24
|
+
if ext_name.length > 1
|
25
|
+
l = ext_name.length
|
26
|
+
@to_dir_name = ext_name[1..l]
|
27
|
+
else
|
28
|
+
@to_dir_name = "unknown"
|
29
|
+
end
|
30
|
+
@to_dir_path = File.dirname(@from_path) + '/' + @to_dir_name
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_directory
|
34
|
+
unless File.directory?(@to_dir_path)
|
35
|
+
puts "Create directory #{@to_dir_name}"
|
36
|
+
FileUtils.mkdir_p(@to_dir_path)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def get_new_file_name to_path, file_name
|
41
|
+
to_full_path = to_path
|
42
|
+
new_file_name = file_name
|
43
|
+
if File.exist?(to_full_path)
|
44
|
+
i = 1
|
45
|
+
loop do
|
46
|
+
l = file_name.length - @to_dir_name.length - 2
|
47
|
+
new_file_name = "#{file_name[0..l]}_#{i}.#{@to_dir_name}"
|
48
|
+
to_full_path = "#{File.dirname(@from_path)}/#{@to_dir_name}/#{new_file_name}"
|
49
|
+
break unless File.exist?("#{to_full_path}")
|
50
|
+
i += 1
|
51
|
+
end
|
52
|
+
end
|
53
|
+
new_file_name
|
54
|
+
end
|
55
|
+
|
56
|
+
def rename_and_move_files new_file_name
|
57
|
+
unless @from_file == new_file_name
|
58
|
+
FileUtils.mv(@from_file, new_file_name)
|
59
|
+
p "dup file [#{@from_file}] renamed into [#{new_file_name}]"
|
60
|
+
end
|
61
|
+
FileUtils.mv("#{File.dirname(@from_path)}/#{new_file_name}", @to_dir_path)
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative 'file_sorter_tool/file_sorter.rb'
|
2
|
+
|
3
|
+
module FileSorterTool
|
4
|
+
VERSION = '0.1.0'
|
5
|
+
|
6
|
+
def self.version
|
7
|
+
version
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.process_file file_name
|
11
|
+
@file_sorter = FileSorterTool::FileSorter.new file_name
|
12
|
+
@file_sorter.process
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.process_dir path
|
16
|
+
Dir.chdir(path)
|
17
|
+
Dir['*'].each.with_index do |file, index|
|
18
|
+
FileSorterTool::process_file file
|
19
|
+
end
|
20
|
+
puts "Files sorting success..."
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_sorter_tool
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrey Torlopov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple gem that sorting files in selected directory
|
14
|
+
email: torlopov.andrey@gmail.com
|
15
|
+
executables:
|
16
|
+
- file_sorter_tool
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- Rakefile
|
21
|
+
- bin/file_sorter_tool
|
22
|
+
- lib/file_sorter_tool.rb
|
23
|
+
- lib/file_sorter_tool/file_sorter.rb
|
24
|
+
homepage: http://rubygems.org/gems/file_sorter_tool
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.6.8
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Sort files in selected directory
|
48
|
+
test_files: []
|