file_renamer 0.1.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/.byebug_history +26 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/MCVE +8 -0
- data/README.md +92 -0
- data/Rakefile +5 -0
- data/bin/filerenamer +25 -0
- data/bin/setup +5 -0
- data/file_renamer.gemspec +27 -0
- data/lib/file_renamer.rb +80 -0
- data/lib/file_renamer/version.rb +3 -0
- data/lib/path.rb +56 -0
- data/spec/integral_spec.rb +85 -0
- data/spec/path_spec.rb +79 -0
- data/spec/renamer_spec.rb +126 -0
- data/spec/spec_helper.rb +3 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4a503af7cf467c147bfb29563cdaf00f93a49b8c5cfc9a99746285032cd94aa9
|
4
|
+
data.tar.gz: 26323c8c8b0903cb4a7b8bd923a6f256c860b0e54204dd20c41425de541717b6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 361e5dfff661bcfcfc82abd066d00251e7826d02cce30e2970f7ee88c79c50c207d239eec4b1c968b78820c9dace3d1f4e1337a113683d1163b46dae19e3bb3b
|
7
|
+
data.tar.gz: 362f8b9fadc5e3c5c5cca1dac715d8843300fbda5905692571e5806649548f12e1528256259fd618795f422bc0411bb88bfd70a470d0ce69435affbae164c3e4
|
data/.byebug_history
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
exit
|
2
|
+
filename.gsub(/^\w+./, "#{name}")exi
|
3
|
+
filename.gsub(/^\w+./, "#{name}")
|
4
|
+
name = "new_name"
|
5
|
+
name =
|
6
|
+
filename = "/Documents/IMG_123.jpg".slice(FileRenamer::Path::FILENAME_REGEX)
|
7
|
+
@path
|
8
|
+
with_ext_params
|
9
|
+
name
|
10
|
+
|
11
|
+
FileRenamer::Path.FILENAME_REGEX
|
12
|
+
FileRenamer::Path
|
13
|
+
require 'path'
|
14
|
+
require './lib/path.rb'
|
15
|
+
require './lib/path'
|
16
|
+
require 'lib/path'
|
17
|
+
require './lib/path'
|
18
|
+
require '../lib/path.rb'
|
19
|
+
require './lib/path.rb'
|
20
|
+
require 'lib/path.rb'
|
21
|
+
require 'lib/path'
|
22
|
+
require 'path'
|
23
|
+
with_ext_params.send(:renamed_path, 0)
|
24
|
+
with_ext_params
|
25
|
+
exit
|
26
|
+
n
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Taras Zhuk
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/MCVE
ADDED
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# FileRenamer
|
2
|
+
|
3
|
+
Simple bulk file renaming tool.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'file_renamer'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install file_renamer
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### From terminal
|
24
|
+
|
25
|
+
filerenamer [options]
|
26
|
+
-d --dir (optional)
|
27
|
+
-p --prefix (optional)
|
28
|
+
-e --extension (optional)
|
29
|
+
-n --name (required)
|
30
|
+
|
31
|
+
- Use `--dir` key to specify directory where you want script to be ran (not required in terminal version)
|
32
|
+
|
33
|
+
*IMPORTANT!
|
34
|
+
If no `--dir` passed to parameters, directory where script where executed will be proceed.*
|
35
|
+
|
36
|
+
- Use `--name` key to specify `new_name` for the files that should be renamed
|
37
|
+
|
38
|
+
First will be named `new_name`, second - `new_name_1`, third - `new_name_2` etc.
|
39
|
+
|
40
|
+
- Use `--prefix` key to specify prefix for the filenames that should be renamed
|
41
|
+
|
42
|
+
If `file` passed to `--prefix`
|
43
|
+
and directory contains 3 files: `file1`, `file2`, `picture3`
|
44
|
+
Only `file1` and `file2` will be renamed, `picture3` will stay untouched.
|
45
|
+
|
46
|
+
- Use `--ext` key to specify file extension for the filenames that should be renamed
|
47
|
+
|
48
|
+
If `jpg`(dot is not required) passed to `--ext`
|
49
|
+
and directory contains 3 files: `file1.txt`, `file2.jpg`, `picture3.jpg`
|
50
|
+
Only `file2.jpg` and `picture3.jpg` will be renamed, `file1.txt` will stay untouched.
|
51
|
+
|
52
|
+
*IMPORTANT!*
|
53
|
+
*- If no prefix neither extension passed to the script EVERY file in directory will be renamed, except directories.*
|
54
|
+
|
55
|
+
*HINT:*
|
56
|
+
*- You can mix --ext and --prefix to concretize conditions*
|
57
|
+
|
58
|
+
### In your project
|
59
|
+
|
60
|
+
FileRenamer::Renamer.rename!(params)
|
61
|
+
where
|
62
|
+
- params[:dir] - for execution directory (required)
|
63
|
+
- params[:prefix] - for filename's prefixes
|
64
|
+
- params[:ext] - for filename's extensions
|
65
|
+
- params[:name] - for filename's new names (required)
|
66
|
+
|
67
|
+
## Development
|
68
|
+
|
69
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
70
|
+
|
71
|
+
TODO: extension change
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it ( https://github.com/[my-github-username]/file_renamer/fork )
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create a new Pull Request
|
80
|
+
|
81
|
+
## Spec
|
82
|
+
|
83
|
+
```
|
84
|
+
rake spec_all
|
85
|
+
```
|
86
|
+
in project folder to run all specs
|
87
|
+
|
88
|
+
## License
|
89
|
+
|
90
|
+
MIT License.
|
91
|
+
|
92
|
+
2020 Tee Zed
|
data/Rakefile
ADDED
data/bin/filerenamer
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require_relative '../lib/file_renamer'
|
5
|
+
require_relative '../lib/path'
|
6
|
+
|
7
|
+
begin
|
8
|
+
|
9
|
+
params = {}
|
10
|
+
OptionParser.new do |opts|
|
11
|
+
opts.on('-n', '--name STRING', String)
|
12
|
+
opts.on('-p', '--prefix STRING', String)
|
13
|
+
opts.on('-e', '--ext STRING', String)
|
14
|
+
opts.on('-d', '--dir STRING', String)
|
15
|
+
end.parse!(into: params)
|
16
|
+
|
17
|
+
rescue OptionParser::MissingArgument
|
18
|
+
puts "Argument error."
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
FileRenamer::Renamer.rename!(params)
|
24
|
+
|
25
|
+
puts "#{FileRenamer::Path.get_count} paths renamed!"
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'file_renamer/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "file_renamer"
|
9
|
+
spec.version = FileRenamer::VERSION
|
10
|
+
spec.authors = ["Taras Zhuk"]
|
11
|
+
spec.email = ["tee@zed@gmail.com"]
|
12
|
+
|
13
|
+
spec.platform = Gem::Platform::RUBY
|
14
|
+
spec.summary = %q{Multiple files renamer script.}
|
15
|
+
spec.description = %q{File Renamer allows to rename bunch of files of similar extension or filename prefix all at once.}
|
16
|
+
spec.homepage = "https://github.com/tee0zed/file_renamer"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.files = `git ls-files`.split($/)
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.executables = 'filerenamer'
|
22
|
+
spec.require_paths = ["lib", "bin"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
end
|
data/lib/file_renamer.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require_relative 'file_renamer/version.rb'
|
2
|
+
require_relative 'path'
|
3
|
+
|
4
|
+
module FileRenamer
|
5
|
+
class Renamer
|
6
|
+
SLASH = Gem.win_platform? ? '\\' : '/'
|
7
|
+
FILENAME_REGEXP = /^[a-zA-Z_0-9 -]+$/
|
8
|
+
EXTENSION_REGEXP = /^[a-zA-Z0-9]{1,4}$/
|
9
|
+
|
10
|
+
attr_reader :params, :paths
|
11
|
+
|
12
|
+
def self.rename!(params)
|
13
|
+
session = FileRenamer::Renamer.new(params)
|
14
|
+
session.get_paths
|
15
|
+
session.rename_files
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(params)
|
19
|
+
@params = params
|
20
|
+
@paths = []
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_paths
|
24
|
+
params_correction!
|
25
|
+
|
26
|
+
Dir["#{params[:dir]}*"].sort.each do |path|
|
27
|
+
@paths << FileRenamer::Path.new(path, params)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def rename_files
|
32
|
+
@paths.each { |p| p.rename_file! if p.correct_path? }
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def params_correction!
|
38
|
+
correct_dir
|
39
|
+
|
40
|
+
abort "New name must exist!" unless params[:name]
|
41
|
+
correct_name
|
42
|
+
|
43
|
+
correct_prefix
|
44
|
+
correct_ext
|
45
|
+
|
46
|
+
abort "Incorrect name!" unless @params[:name]
|
47
|
+
abort "Directory must exist!" unless @params[:dir]
|
48
|
+
end
|
49
|
+
|
50
|
+
def correct_dir
|
51
|
+
if @params[:dir].nil?
|
52
|
+
@params[:dir] = Dir.pwd
|
53
|
+
@params[:dir] << SLASH
|
54
|
+
else
|
55
|
+
@params[:dir] << SLASH unless @params[:dir][-1] == SLASH
|
56
|
+
@params[:dir] = nil unless File.directory?(@params[:dir])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def correct_name
|
61
|
+
if @params[:name] && @params[:name].match?(FILENAME_REGEXP)
|
62
|
+
@params[:name] = @params[:name].strip.gsub(' ', '_')
|
63
|
+
else
|
64
|
+
@params[:name] = nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def correct_prefix
|
69
|
+
@params[:prefix] = @params[:prefix].strip if @params[:prefix]
|
70
|
+
end
|
71
|
+
|
72
|
+
def correct_ext
|
73
|
+
if @params[:ext] && @params[:ext].match?(EXTENSION_REGEXP)
|
74
|
+
@params[:ext] = '.' + @params[:ext] unless @params[:ext][0] == '.'
|
75
|
+
else
|
76
|
+
@params[:ext] = nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/lib/path.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'byebug'
|
2
|
+
|
3
|
+
module FileRenamer
|
4
|
+
class Path
|
5
|
+
FILENAME_REGEX = /([^\/|\\]+$)/
|
6
|
+
|
7
|
+
@@counter = 0
|
8
|
+
|
9
|
+
attr_reader :path, :prefix, :ext, :new_name
|
10
|
+
|
11
|
+
def self.get_count
|
12
|
+
@@counter
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.reset_counter
|
16
|
+
@@counter = 0
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(path, params)
|
20
|
+
@path = path
|
21
|
+
@new_name = params[:name]
|
22
|
+
@prefix = params[:prefix]
|
23
|
+
@ext = params[:ext]
|
24
|
+
end
|
25
|
+
|
26
|
+
def correct_path?
|
27
|
+
@path.slice(FILENAME_REGEX)
|
28
|
+
.match?(Regexp.new("^#{prefix}.*#{ext}$")) &&
|
29
|
+
!File.directory?(@path)
|
30
|
+
end
|
31
|
+
|
32
|
+
def rename_file!
|
33
|
+
File.rename(@path, renamed_path(@@counter))
|
34
|
+
@@counter += 1
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def renamed_path(num)
|
40
|
+
filename = @path.slice(FILENAME_REGEX)
|
41
|
+
|
42
|
+
name =
|
43
|
+
if num == 0
|
44
|
+
"#{@new_name}"
|
45
|
+
else
|
46
|
+
"#{@new_name}_#{num}"
|
47
|
+
end
|
48
|
+
|
49
|
+
name << '.' if filename.match?('\.')
|
50
|
+
|
51
|
+
new_name = filename.gsub(/^\w+./, "#{name}")
|
52
|
+
|
53
|
+
@path.gsub(filename, new_name)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "FileRenamer" do
|
4
|
+
context "integral test" do
|
5
|
+
before(:each) {
|
6
|
+
i = 1
|
7
|
+
3.times do
|
8
|
+
File.write("./spec/fixtures/dir/file#{i}.txt", "")
|
9
|
+
i += 1
|
10
|
+
end
|
11
|
+
|
12
|
+
i = 1
|
13
|
+
3.times do
|
14
|
+
File.write("./spec/fixtures/dir/pict#{i}.jpg", "")
|
15
|
+
i += 1
|
16
|
+
end
|
17
|
+
}
|
18
|
+
|
19
|
+
after(:each) {
|
20
|
+
Dir["./spec/fixtures/dir/*"].each do |f|
|
21
|
+
File.delete(f) if File.exists?(f)
|
22
|
+
end
|
23
|
+
}
|
24
|
+
|
25
|
+
let(:params1) {{ dir: "./spec/fixtures/dir/", prefix: "file", ext: "txt", name: "new_name" }}
|
26
|
+
it 'renames files correctly' do
|
27
|
+
FileRenamer::Path.reset_counter
|
28
|
+
FileRenamer::Renamer.rename!(params1)
|
29
|
+
expect(Dir["./spec/fixtures/dir/*"]).to match_array ["./spec/fixtures/dir/new_name.txt",
|
30
|
+
"./spec/fixtures/dir/new_name_1.txt",
|
31
|
+
"./spec/fixtures/dir/new_name_2.txt",
|
32
|
+
"./spec/fixtures/dir/pict1.jpg",
|
33
|
+
"./spec/fixtures/dir/pict2.jpg",
|
34
|
+
"./spec/fixtures/dir/pict3.jpg"]
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:params2) {{ dir: "./spec/fixtures/dir/", prefix: "pict", ext: "txt", name: "new_name" }}
|
38
|
+
it 'doesnt renames if prefix and ext doesnt match any of files' do
|
39
|
+
FileRenamer::Path.reset_counter
|
40
|
+
FileRenamer::Renamer.rename!(params2)
|
41
|
+
expect(Dir["./spec/fixtures/dir/*"]).to match_array ["./spec/fixtures/dir/file1.txt",
|
42
|
+
"./spec/fixtures/dir/file2.txt",
|
43
|
+
"./spec/fixtures/dir/file3.txt",
|
44
|
+
"./spec/fixtures/dir/pict1.jpg",
|
45
|
+
"./spec/fixtures/dir/pict2.jpg",
|
46
|
+
"./spec/fixtures/dir/pict3.jpg"]
|
47
|
+
end
|
48
|
+
|
49
|
+
let(:params3) {{ dir: "./spec/fixtures/dir/", prefix: nil, ext: "jpg", name: "new_name" }}
|
50
|
+
it 'renames files correctly if only ext passed' do
|
51
|
+
FileRenamer::Path.reset_counter
|
52
|
+
FileRenamer::Renamer.rename!(params3)
|
53
|
+
expect(Dir["./spec/fixtures/dir/*"]).to match_array ["./spec/fixtures/dir/file1.txt",
|
54
|
+
"./spec/fixtures/dir/file2.txt",
|
55
|
+
"./spec/fixtures/dir/file3.txt",
|
56
|
+
"./spec/fixtures/dir/new_name.jpg",
|
57
|
+
"./spec/fixtures/dir/new_name_1.jpg",
|
58
|
+
"./spec/fixtures/dir/new_name_2.jpg"]
|
59
|
+
end
|
60
|
+
|
61
|
+
let(:params4) {{ dir: "./spec/fixtures/dir/", prefix: "file", ext: "jpg", name: "new_name" }}
|
62
|
+
it 'doesnt renames if prefix and ext doesnt match any of files' do
|
63
|
+
FileRenamer::Path.reset_counter
|
64
|
+
FileRenamer::Renamer.rename!(params4)
|
65
|
+
expect(Dir["./spec/fixtures/dir/*"]).to match_array ["./spec/fixtures/dir/file1.txt",
|
66
|
+
"./spec/fixtures/dir/file2.txt",
|
67
|
+
"./spec/fixtures/dir/file3.txt",
|
68
|
+
"./spec/fixtures/dir/pict1.jpg",
|
69
|
+
"./spec/fixtures/dir/pict2.jpg",
|
70
|
+
"./spec/fixtures/dir/pict3.jpg"]
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:params5) {{ dir: "./spec/fixtures/dir/", prefix: 'pict', ext: nil, name: "new_name" }}
|
74
|
+
it 'renames files correctly if only prefix passed' do
|
75
|
+
FileRenamer::Path.reset_counter
|
76
|
+
FileRenamer::Renamer.rename!(params5)
|
77
|
+
expect(Dir["./spec/fixtures/dir/*"]).to match_array ["./spec/fixtures/dir/file1.txt",
|
78
|
+
"./spec/fixtures/dir/file2.txt",
|
79
|
+
"./spec/fixtures/dir/file3.txt",
|
80
|
+
"./spec/fixtures/dir/new_name.jpg",
|
81
|
+
"./spec/fixtures/dir/new_name_1.jpg",
|
82
|
+
"./spec/fixtures/dir/new_name_2.jpg"]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/spec/path_spec.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'byebug'
|
3
|
+
describe "FileRenamer" do
|
4
|
+
context "Path#correct_path?" do
|
5
|
+
let(:params1) {{ name: 'new_name', prefix: nil, ext: nil }}
|
6
|
+
let(:no_opt_params1) { FileRenamer::Path.new('/Documents/DCIM_123.jpg', params1)}
|
7
|
+
let(:no_opt_params2) { FileRenamer::Path.new('/Documents/ANOTHER_1123133.jpg', params1)}
|
8
|
+
it "return true for all paths without options" do
|
9
|
+
expect(no_opt_params1.correct_path?).to eq true
|
10
|
+
expect(no_opt_params2.correct_path?).to eq true
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:params2) {{ name: 'new_name', prefix: 'DCIM', ext: '.jpg' }}
|
14
|
+
let(:with_options_params_correct1) { FileRenamer::Path.new('/Documents/DCIM_1asdasdggqeda23.jpg', params2) }
|
15
|
+
let(:with_options_params_correct2) { FileRenamer::Path.new('/Documents/DCIM_1f12fsa23.jpg', params2) }
|
16
|
+
it "return true if options matches path" do
|
17
|
+
expect(with_options_params_correct1.correct_path?).to eq true
|
18
|
+
expect(with_options_params_correct2.correct_path?).to eq true
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:params3) {{ name: 'new_name', prefix: 'DCIM', ext: nil }}
|
22
|
+
let(:with_prefix_correct1) { FileRenamer::Path.new('/Documents/DCIM_1asdads123223.jpg', params3) }
|
23
|
+
let(:with_prefix_correct2) { FileRenamer::Path.new('/Documents/DCIM_1addsa23.png', params3) }
|
24
|
+
it "return true if prefix matches path" do
|
25
|
+
expect(with_prefix_correct1.correct_path?).to eq true
|
26
|
+
expect(with_prefix_correct2.correct_path?).to eq true
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:params4) {{ name: 'new_name', prefix: nil, ext: '.jpg' }}
|
30
|
+
let(:with_ext_correct1) { FileRenamer::Path.new('/Documents/ANOTHER_124143.jpg', params4) }
|
31
|
+
let(:with_ext_correct2) { FileRenamer::Path.new('/Documents/IMG_12122133.jpg', params4) }
|
32
|
+
it "return true if extension matches path" do
|
33
|
+
expect(with_ext_correct1.correct_path?).to eq true
|
34
|
+
expect(with_ext_correct2.correct_path?).to eq true
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:params5) {{ name: 'new_name', prefix: 'DCIM', ext: '.jpg' }}
|
38
|
+
let(:with_prefix_incorrect1) { FileRenamer::Path.new('/Documents/IDCIMMG_114223.jpg', params5) }
|
39
|
+
let(:with_prefix_incorrect2) { FileRenamer::Path.new('/Documents/IMGDCIMDCIM_12131123.jpg', params5) }
|
40
|
+
it "return false if prefix not matches path" do
|
41
|
+
expect(with_prefix_incorrect1.correct_path?).to eq false
|
42
|
+
expect(with_prefix_incorrect2.correct_path?).to eq false
|
43
|
+
end
|
44
|
+
|
45
|
+
let(:params6) {{ name: 'new_name', prefix: 'IMG', ext: '.jpg' }}
|
46
|
+
let(:with_ext_incorrect1) { FileRenamer::Path.new('/Documents/IMG_123.png', params6) }
|
47
|
+
let(:with_ext_incorrect2) { FileRenamer::Path.new('/Documents/IMG_123.txt', params6) }
|
48
|
+
it "return false if extension not matches path" do
|
49
|
+
expect(with_ext_incorrect1.correct_path?).to eq false
|
50
|
+
expect(with_ext_incorrect2.correct_path?).to eq false
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'Path#renamed_path' do
|
55
|
+
let(:params7) {{ name: 'new_name', prefix: nil, ext: '.jpg' }}
|
56
|
+
let(:with_ext_params) { FileRenamer::Path.new('/Documents/IMG_123.jpg', params7) }
|
57
|
+
it 'renames file correctly if extension param passed' do
|
58
|
+
expect(with_ext_params.send(:renamed_path, 0)).to eq "/Documents/new_name.jpg"
|
59
|
+
end
|
60
|
+
|
61
|
+
let(:params8) {{ name: 'new_name', prefix: nil, ext: nil }}
|
62
|
+
let(:without_ext_params) { FileRenamer::Path.new('/Documents/IMG_123.jpg', params8) }
|
63
|
+
it 'renames file correctly if extension param not passed' do
|
64
|
+
expect(without_ext_params.send(:renamed_path, 0)).to eq "/Documents/new_name.jpg"
|
65
|
+
end
|
66
|
+
|
67
|
+
let(:params9) {{ name: 'new_name', prefix: nil, ext: nil }}
|
68
|
+
let(:filename_w_two_dots_with_ext_params) { FileRenamer::Path.new('/Documents/IMG_123.html.erb', params9) }
|
69
|
+
it 'renames file with two extensions correctly if extension param passed' do
|
70
|
+
expect(filename_w_two_dots_with_ext_params.send(:renamed_path, 0)).to eq "/Documents/new_name.html.erb"
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:params10) {{ name: 'new_name', prefix: 'DCIM', ext: '.jpg' }}
|
74
|
+
let(:filename_w_two_dots_without_ext_params) { FileRenamer::Path.new('/Documents/IMG_123.html.erb', params10) }
|
75
|
+
it 'renames file with two extensions correctly if extension param not passed' do
|
76
|
+
expect(filename_w_two_dots_without_ext_params.send(:renamed_path, 0)).to eq "/Documents/new_name.html.erb"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'FileRenamer' do
|
4
|
+
context 'Renamer#get_paths' do
|
5
|
+
before(:each) {
|
6
|
+
i = 1
|
7
|
+
3.times do
|
8
|
+
File.write("./spec/fixtures/dir/file#{i}.txt", "")
|
9
|
+
i += 1
|
10
|
+
end
|
11
|
+
|
12
|
+
i = 1
|
13
|
+
3.times do
|
14
|
+
File.write("./spec/fixtures/dir/pict#{i}.jpg", "")
|
15
|
+
i += 1
|
16
|
+
end
|
17
|
+
}
|
18
|
+
|
19
|
+
after(:each) {
|
20
|
+
Dir["./spec/fixtures/dir/*"].each do |f|
|
21
|
+
File.delete(f) if File.exists? f
|
22
|
+
end
|
23
|
+
}
|
24
|
+
|
25
|
+
let(:params1) {{ dir: "./spec/fixtures/dir/", ext: nil, prefix: nil, name: 'new_name' }}
|
26
|
+
let(:obj1) { FileRenamer::Renamer.new(params1) }
|
27
|
+
it 'returns all Path objects' do
|
28
|
+
obj1.get_paths
|
29
|
+
|
30
|
+
expect(obj1.paths.count).to eq 6
|
31
|
+
expect(obj1.paths.all?{|o| o.is_a? FileRenamer::Path }).to eq true
|
32
|
+
expect(obj1.paths.map(&:path)).to match_array ["./spec/fixtures/dir/pict3.jpg",
|
33
|
+
"./spec/fixtures/dir/pict2.jpg",
|
34
|
+
"./spec/fixtures/dir/pict1.jpg",
|
35
|
+
"./spec/fixtures/dir/file1.txt",
|
36
|
+
"./spec/fixtures/dir/file2.txt",
|
37
|
+
"./spec/fixtures/dir/file3.txt" ]
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'Renamer#correct_ext' do
|
43
|
+
let(:params) {{ ext: 'jpg' }}
|
44
|
+
let(:obj) { FileRenamer::Renamer.new(params) }
|
45
|
+
it 'return ext string with dot if ext not nil' do
|
46
|
+
obj.send(:correct_ext)
|
47
|
+
expect(obj.params[:ext]).to eq '.jpg'
|
48
|
+
end
|
49
|
+
|
50
|
+
let(:params1) {{ ext: 'jpganyany' }}
|
51
|
+
let(:obj1) { FileRenamer::Renamer.new(params1) }
|
52
|
+
it 'return nil if ext not matches regexp' do
|
53
|
+
obj1.send(:correct_ext)
|
54
|
+
expect(obj1.params[:ext]).to eq nil
|
55
|
+
end
|
56
|
+
|
57
|
+
let(:params2) {{ ext: nil }}
|
58
|
+
let(:obj2) { FileRenamer::Renamer.new(params2) }
|
59
|
+
it 'return nil if ext nil' do
|
60
|
+
obj2.send(:correct_ext)
|
61
|
+
expect(obj2.params[:ext]).to eq nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'Renamer#correct_dir' do
|
66
|
+
let(:params) {{ dir: './spec/fixtures/dir/' }}
|
67
|
+
let(:obj) { FileRenamer::Renamer.new(params) }
|
68
|
+
it 'return dir string if dir exists' do
|
69
|
+
obj.send(:correct_dir)
|
70
|
+
expect(obj.params[:dir]).to eq './spec/fixtures/dir/'
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:params1) {{ dir: './spec/fixtures/dir2/' }}
|
74
|
+
let(:obj1) { FileRenamer::Renamer.new(params1) }
|
75
|
+
it 'return nil if dir does not exists' do
|
76
|
+
obj1.send(:correct_dir)
|
77
|
+
expect(obj1.params[:dir]).to eq nil
|
78
|
+
end
|
79
|
+
|
80
|
+
let(:params2) {{ dir: './spec/fixtures/dir' }}
|
81
|
+
let(:obj2) { FileRenamer::Renamer.new(params2) }
|
82
|
+
it 'add slash to the end of path' do
|
83
|
+
obj2.send(:correct_dir)
|
84
|
+
expect(obj2.params[:dir]).to eq './spec/fixtures/dir/'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'Renamer#correct_prefix' do
|
89
|
+
let(:params) {{ prefix: " DCIM\n" }}
|
90
|
+
let(:obj) { FileRenamer::Renamer.new(params) }
|
91
|
+
it 'strips prefix string if prefix exists' do
|
92
|
+
obj.send(:correct_prefix)
|
93
|
+
expect(obj.params[:prefix]).to eq 'DCIM'
|
94
|
+
end
|
95
|
+
|
96
|
+
let(:params1) {{ prefix: nil }}
|
97
|
+
let(:obj1) { FileRenamer::Renamer.new(params1) }
|
98
|
+
it 'return nil if prefix does not exists' do
|
99
|
+
obj1.send(:correct_prefix)
|
100
|
+
expect(obj1.params[:prefix]).to eq nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'Renamer#correct_name' do
|
105
|
+
let(:params) {{ name: "new name " }}
|
106
|
+
let(:obj) { FileRenamer::Renamer.new(params) }
|
107
|
+
it 'deletes spaces from filename and strips' do
|
108
|
+
obj.send(:correct_name)
|
109
|
+
expect(obj.params[:name]).to eq 'new_name'
|
110
|
+
end
|
111
|
+
|
112
|
+
let(:params1) {{ name: nil }}
|
113
|
+
let(:obj1) { FileRenamer::Renamer.new(params1) }
|
114
|
+
it 'return nil if name does not exists' do
|
115
|
+
obj1.send(:correct_name)
|
116
|
+
expect(obj1.params[:name]).to eq nil
|
117
|
+
end
|
118
|
+
|
119
|
+
let(:params2) {{ name: 'name any any ////&&$^' }}
|
120
|
+
let(:obj2) { FileRenamer::Renamer.new(params1) }
|
121
|
+
it 'return nil if name does not matches regexp' do
|
122
|
+
obj1.send(:correct_name)
|
123
|
+
expect(obj1.params[:name]).to eq nil
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_renamer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Taras Zhuk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: File Renamer allows to rename bunch of files of similar extension or
|
56
|
+
filename prefix all at once.
|
57
|
+
email:
|
58
|
+
- tee@zed@gmail.com
|
59
|
+
executables:
|
60
|
+
- filerenamer
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".byebug_history"
|
65
|
+
- ".gitignore"
|
66
|
+
- ".rspec"
|
67
|
+
- ".travis.yml"
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- MCVE
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- bin/filerenamer
|
74
|
+
- bin/setup
|
75
|
+
- file_renamer.gemspec
|
76
|
+
- lib/file_renamer.rb
|
77
|
+
- lib/file_renamer/version.rb
|
78
|
+
- lib/path.rb
|
79
|
+
- spec/integral_spec.rb
|
80
|
+
- spec/path_spec.rb
|
81
|
+
- spec/renamer_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
homepage: https://github.com/tee0zed/file_renamer
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
- bin
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.7.3
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Multiple files renamer script.
|
108
|
+
test_files:
|
109
|
+
- spec/integral_spec.rb
|
110
|
+
- spec/path_spec.rb
|
111
|
+
- spec/renamer_spec.rb
|
112
|
+
- spec/spec_helper.rb
|