Este es mi primer script en Ruby, Y lo he hecho con mucha ayuda sino no habría podido
Y me gustaría simplificar la comprobacion de los argumentos así por ejemplo:
Código
if (ARGV[0])==(-h|--help)
help()
end
Pero no se hacerlo bien xD
Si ven algún error o mejora diganmelo, gracias
EDITO:
Por cierto, Me parece tremendamente inseguro que al usar:
File.rename
Si existe un archivo con el mismo nombre que el archivo nuevo (renombrado), El archivo se reemplaza por el renombrado, En vez de dar error... O algo parecido xD
Código
# -*- coding: UTF-8 -*-
# Renombrador preconfigurado de archivos
#
# El código original es de RyogiShiki
# http://foro.elhacker.net/scripting/solucionado_ruby_renombrando_un_caracter_ilegal-t354066.0.html
# Gemas, Módulos...
require 'find'
exit if Object.const_defined?(:Ocra)
system('chcp 1252 >NUL')
# Métodos
def reset_vars()
$total = -1
$renamed = 0
end
def resultado()
puts " Procesados: #{$total} archivos"
puts " Renombrados: #{$renamed} archivos"
system('chcp 850 >NUL')
Process.exit
end
def advise()
print ' Use "Renamer.exe -a" Para mostrar la ayuda.' + "\n"
system('chcp 850 >NUL')
Process.exit
end
def help()
system('chcp 850 >NUL')
print "\n Modo de empleo:\n\n"
print " " + __FILE__.split('/').last + " [Opci\u00F3n] [Ruta]\n\n"
print "\n Opciones: \n\n"
print " -c --comilla Reemplaza \[\u00B4\] por \[\u0027\]\n\n"
print " -e --extension Reemplaza [ .mp3] por [.mp3]\n\n"
print " -f --featuring Reemplaza [ ft ],[ ft. ],[ feat ],[ featuring ] por [ feat. ]\n\n"
print " -g --guion Reemplaza \[\u2013\] por \[-\]\n\n"
print " -i --interrogante Elimina \[\u00BF\]\n\n"
print " -t --todo Combina todas las opciones (-c + -e + -f + -g + -i)\n"
Process.exit
end
def reemplazar(caracter_a_reemplazar, nuevo_caracter)
$total = -1
Find.find(ARGV[1].gsub("\\", "/")) { |path|
path = path.encode('utf-8')
if path[caracter_a_reemplazar] then
if File.exist?(path.gsub(caracter_a_reemplazar, nuevo_caracter))
print "\n ERROR. El archivo a reemplazar ya existe: " + (path).split('/').last + "\n"
else
File.rename(path, path.gsub(caracter_a_reemplazar, nuevo_caracter))
$renamed += 1
end
end
$total += 1
}
end
# Argumentos
if (ARGV.empty?) then
help()
end
if (ARGV[0])=="-a" or ARGV[0] == "/?"
help()
end
if (ARGV[1])==()
print "\n ERROR. Debe introducir una ruta...\n\n"
advise()
elsif if not File.directory? (ARGV[1]) then
print "\n ERROR. La ruta no existe...\n\n"
advise()
end
end
if ARGV[0] == "-c" or ARGV[0] == "--comilla"
reset_vars()
reemplazar("\u00B4", "\u0027")
resultado()
elsif (ARGV[0])=="-e" or ARGV[0] == "--extension"
reset_vars()
reemplazar(" .mp3", ".mp3")
reemplazar(" .MP3", ".mp3")
reemplazar(" .Mp3", ".mp3")
resultado()
elsif (ARGV[0])=="-f" or ARGV[0] == "--featuring"
reset_vars()
reemplazar(" ft. ", " feat. ")
reemplazar(" Ft. ", " feat. ")
reemplazar(" FT. ", " feat. ")
reemplazar(" ft ", " feat. ")
reemplazar(" Ft ", " feat. ")
reemplazar(" FT ", " feat. ")
reemplazar(" feat ", " feat. ")
reemplazar(" Feat ", " feat. ")
reemplazar(" FEAT ", " feat. ")
reemplazar(" featuring ", " feat. ")
reemplazar(" Featuring ", " feat. ")
reemplazar(" FEATURING ", " feat. ")
resultado()
elsif (ARGV[0])=="-g" or ARGV[0] == "--guion"
reset_vars()
reemplazar("\u2013", "-")
resultado()
elsif (ARGV[0])=="-i" or ARGV[0] == "--interrogante"
reset_vars()
reemplazar("\u00BF", "")
resultado()
elsif (ARGV[0])=="-t" or ARGV[0] == "--todo"
reset_vars()
reemplazar("\u00B4", "\u0027")
reemplazar("\u2013", "-")
reemplazar("\u00BF", "")
reemplazar(" ft. ", " feat. ")
reemplazar(" Ft. ", " feat. ")
reemplazar(" FT. ", " feat. ")
reemplazar(" ft ", " feat. ")
reemplazar(" Ft ", " feat. ")
reemplazar(" FT ", " feat. ")
reemplazar(" feat ", " feat. ")
reemplazar(" Feat ", " feat. ")
reemplazar(" FEAT ", " feat. ")
reemplazar(" featuring ", " feat. ")
reemplazar(" Featuring ", " feat. ")
reemplazar(" FEATURING ", " feat. ")
reemplazar(" .mp3", ".mp3")
reemplazar(" .MP3", ".mp3")
reemplazar(" .Mp3", ".mp3")
resultado()
end


 
  




 Autor
 Autor
		




 En línea
									En línea
								










 
						