Last active
August 29, 2015 14:07
-
-
Save emj365/d94e2eed5803f5602efb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
# | |
# this script rename all files in current floder by number sequence | |
# | |
### | |
@start_i = 0 | |
@folder = Dir.pwd | |
class String | |
# colorization | |
def colorize(color_code) | |
"\e[#{color_code}m#{self}\e[0m" | |
end | |
def red | |
colorize(31) | |
end | |
def green | |
colorize(32) | |
end | |
def yellow | |
colorize(33) | |
end | |
def pink | |
colorize(35) | |
end | |
end | |
def rename_files params | |
i = @start_i | |
for file in Dir.entries( @folder ) | |
file_path = @folder + '/' + file; | |
if File.file?( file_path) && File.extname(file_path) == ".#{@ext}" | |
number = i.to_s | |
# number = '0' + i.to_s if i < 10 | |
if params[:mode] == 'test' | |
puts "will rename #{file} to #{number}.#{@ext}" | |
end | |
if params[:mode] == 'prepare' | |
File.rename( file_path, "#{@folder}/script_backup_#{number}.#{@ext}" ) | |
end | |
if params[:mode] == 'force' | |
File.rename( file_path, "#{@folder}/#{number}.#{@ext}" ) | |
end | |
i = i + 1 | |
end | |
end | |
if params[:mode] == 'prepare' | |
rename_files( mode: 'force' ) | |
end | |
end | |
puts "This script will rename all file" | |
puts "in #{@folder}" | |
puts "new name will be a sequence by number.\n" | |
puts "What\'s the file extention? (with out '.') " | |
@ext = gets.strip | |
if @ext.empty? | |
puts "canceled, extention must specify.".red | |
exit | |
end | |
puts "number start: (defalut id 0)" | |
@start_i = gets.strip.to_i | |
rename_files( mode: 'test' ) | |
puts "is that alright? y/n" | |
confirm = gets.strip | |
if confirm == 'y' | |
rename_files( mode: 'prepare' ) | |
puts "done".green | |
else | |
puts "canceled".yellow | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment