(http://img442.imageshack.us/img442/8373/prtscrcapturew.jpg)
Una utilidad por linea de comandos para convertir entre bytes, kilobytes, megabytes, gigabytes, terabytes, y petabytes.
CitarComo usarlo:
CTool.exe {Tamaño} {Unidad} {A unidad}
Las unidades son: Bytes, KB, MB, GB, TB, PB.
Ejemplos:
- Convertir bytes a megabytes:
CTool 446.777.647.104 Bytes MB
- Convertir megabytes a kilobytes:
CTool 44.33 MB KB
- Convertir petabyte a todas las unidades:
CTool 1 PB
Algunas imagenes:
(http://img822.imageshack.us/img822/7444/prtscrcapture2o.jpg) (http://img543.imageshack.us/img543/3342/prtscrcapture3q.jpg)
- El script convertido a EXE:
(http://img254.imageshack.us/img254/8188/download1n.png) (http://www.4shared.com/file/mjPOirX9/CTool.html)
- Un ejemplo de uso en
Batch, Para averiguar el espacio libre en el disco duro:
@Echo OFF
For /f "tokens=3 delims= " %%# in ('Dir ^| find "libres"') do (Set "Bytes=%%#")
For /f "tokens=2 delims==" %%# in ('Ctool %bytes% bytes gb ^| Find "="') do (Echo: Espacio libre en %Homedrive%%%#)
Pause >NUL
- El script, Codeado en Ruby:
PD: Quizás no me ha quedado muy "bonito", Repito bastantes cosas que se podrian simplificar xD pero no estoy por la labor de hacerlo, Así como está ya funciona xD.
# -*- coding: UTF-8 -*-
# Conversion Tool v0.1
# By Elektro H@cker
exit if Object.const_defined?(:Ocra)
def logo()
puts "
_______ _______ __
| _ || |.-----..-----.| |
|. 1___||.| | || _ || _ || |
|. |___ `-|. |-'|_____||_____||__|
|: 1 | |: |
|::.. . | |::.| Conversion
`-------' `---' Tool
"
end
def help()
print "
How to use it:
#{__FILE__.split('/').last.split('.').first}.exe {Size} {Unit} {To unit}
Units are: Bytes, KB, MB, GB, TB, PB.
Examples:
- Convert bytes to megabytes:
#{__FILE__.split('/').last.split('.').first}.exe 446.777.647.104 Bytes MB
- Convert megabytes to kilobytes:
#{__FILE__.split('/').last.split('.').first}.exe 44.33 MB KB
- Convert petabyte to all:
#{__FILE__.split('/').last.split('.').first}.exe 1 PB
"
Process.exit
end
def BytesToUnit (number)
@KILOBYTE = 1024.0
@MEGABYTE = 1024.0 * 1024.0
@GIGABYTE = 1024.0 * 1024.0 * 1024.0
@TERABYTE = 1024.0 * 1024.0 * 1024.0 * 1024.0
@PETABYTE = 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0
@Bytes = number.to_s + " Bytes"
@Kilos = (number / @KILOBYTE).to_s
if number.to_s.length > 20 and @Kilos.length > 3 and @Kilos.split('.').first.length == 1
@Kilos = "KB too depth"
elsif not @Kilos.include? "-" and not @Kilos[0..3].eql? "0.00"
@Kilos = @Kilos.split('.').first + "." + @Kilos.split('.').last[0..1] + " KB"
elsif
@Kilos = "0.0 KB".to_s
end # Kilos
@Megas = (number / @MEGABYTE).to_s
if number.to_s.length > 20 and @Megas.length > 3 and @Megas.split('.').first.length == 1
@Megas = "MB too depth"
elsif not @Megas.include? "-" and not @Megas[0..3].eql? "0.00"
@Megas = @Megas.split('.').first + "." + @Megas.split('.').last[0..1] + " MB"
elsif
@Megas = "0.0 MB".to_s
end # Megas
@Gigas = (number / @GIGABYTE).to_s
if number.to_s.length > 20 and @Gigas.length > 3 and @Gigas.split('.').first.length == 1
@Gigas = "GB too depth"
elsif not @Gigas.include? "-"
@Gigas = @Gigas.split('.').first + "." + @Gigas.split('.').last[0..1] + " GB"
elsif
@Gigas = "0.0 GB".to_s
end # Gigas
@Teras = (number / @TERABYTE).to_s
if number.to_s.length > 20 and @Teras.length > 3 and @Teras.split('.').first.length == 1
@Teras = "TB too depth"
elsif not @Teras.include? "-"
@Teras = @Teras.split('.').first + "." + @Teras.split('.').last[0..1] + " TB"
elsif
@Teras = "0.0 TB".to_s
end # Teras
@Petas = (number / @PETABYTE).to_s
if number.to_s.length > 20 and @Petas.length > 3 and @Petas.split('.').first.length == 1
@Petas = "PB too depth"
elsif not @Petas.include? "-"
@Petas = @Petas.split('.').first + "." + @Petas.split('.').last[0..1] + " PB"
elsif
@Petas = "0.0 PB".to_s
end # Petas
end
def KBToUnit (number)
@BYTE = 1024.0
@MEGABYTE = 1024.0
@GIGABYTE = 1024.0 * 1024.0
@TERABYTE = 1024.0 * 1024.0 * 1024.0
@PETABYTE = 1024.0 * 1024.0 * 1024.0 * 1024.0
@Bytes = (number * @BYTE).to_s
if number.to_s.length > 20 and @Bytes.length > 3 and @Bytes.split('.').first.length == 1
@Bytes = "Bytes too depth"
elsif @Bytes[0..2].eql? "0.0"
@Bytes = @Bytes.split('.').first + "." + @Bytes.split('.').last[0..1] + " Bytes"
elsif
@Bytes = @Bytes.split('.').first + "." + @Bytes.split('.').last[0] + " Bytes"
end # Bytes
@Kilos = number.to_s + " KB"
@Megas = (number / @MEGABYTE).to_s
if number.to_s.length > 20 and @Megas.length > 3 and @Megas.split('.').first.length == 1
@Megas = "MB too depth"
elsif not @Megas.include? "-" and not @Megas[0..3].eql? "0.00"
@Megas = @Megas.split('.').first + "." + @Megas.split('.').last[0..1] + " MB"
elsif
@Megas = "0.0 MB".to_s
end # Megas
@Gigas = (number / @GIGABYTE).to_s
if number.to_s.length > 20 and @Gigas.length > 3 and @Gigas.split('.').first.length == 1
@Gigas = "GB too depth"
elsif not @Gigas.include? "-"
@Gigas = @Gigas.split('.').first + "." + @Gigas.split('.').last[0..1] + " GB"
elsif
@Gigas = "0.0 GB".to_s
end # Gigas
@Teras = (number / @TERABYTE).to_s
if number.to_s.length > 20 and @Teras.length > 3 and @Teras.split('.').first.length == 1
@Teras = "TB too depth"
elsif not @Teras.include? "-"
@Teras = @Teras.split('.').first + "." + @Teras.split('.').last[0..1] + " TB"
elsif
@Teras = "0.0 TB".to_s
end # Teras
@Petas = (number / @PETABYTE).to_s
if number.to_s.length > 20 and @Petas.length > 3 and @Petas.split('.').first.length == 1
@Petas = "PB too depth"
elsif not @Petas.include? "-"
@Petas = @Petas.split('.').first + "." + @Petas.split('.').last[0..1] + " PB"
elsif
@Petas = "0.0 PB".to_s
end # Petas
end
def MBToUnit (number)
@BYTE = 1024.0 * 1024.0
@KILOBYTE = 1024.0
@GIGABYTE = 1024.0
@TERABYTE = 1024.0 * 1024.0
@PETABYTE = 1024.0 * 1024.0 * 1024.0
@Bytes = (number * @BYTE).to_s
if number.to_s.length > 20 and @Bytes.length > 3 and @Bytes.split('.').first.length == 1
@Bytes = "Bytes too depth"
elsif @Bytes[0..2].eql? "0.0"
@Bytes = @Bytes.split('.').first + "." + @Bytes.split('.').last[0..1] + " Bytes"
elsif
@Bytes = @Bytes.split('.').first + "." + @Bytes.split('.').last[0] + " Bytes"
end # Bytes
@Kilos = (number * @KILOBYTE).to_s
if number.to_s.length > 20 and @Kilos.length > 3 and @Kilos.split('.').first.length == 1
@Kilos = "KB too depth"
elsif not @Kilos.include? "-" and not @Kilos[0..3].eql? "0.00"
@Kilos = @Kilos.split('.').first + "." + @Kilos.split('.').last[0..1] + " KB"
elsif
@Kilos = "0.0 KB".to_s
end # Kilos
@Megas = number.to_s + " MB"
@Gigas = (number / @GIGABYTE).to_s
if number.to_s.length > 20 and @Gigas.length > 3 and @Gigas.split('.').first.length == 1
@Gigas = "GB too depth"
elsif not @Gigas.include? "-"
@Gigas = @Gigas.split('.').first + "." + @Gigas.split('.').last[0..1] + " GB"
elsif
@Gigas = "0.0 GB".to_s
end # Gigas
@Teras = (number / @TERABYTE).to_s
if number.to_s.length > 20 and @Teras.length > 3 and @Teras.split('.').first.length == 1
@Teras = "TB too depth"
elsif not @Teras.include? "-"
@Teras = @Teras.split('.').first + "." + @Teras.split('.').last[0..1] + " TB"
elsif
@Teras = "0.0 TB".to_s
end # Teras
@Petas = (number / @PETABYTE).to_s
if number.to_s.length > 20 and @Petas.length > 3 and @Petas.split('.').first.length == 1
@Petas = "PB too depth"
elsif not @Petas.include? "-"
@Petas = @Petas.split('.').first + "." + @Petas.split('.').last[0..1] + " PB"
elsif
@Petas = "0.0 PB".to_s
end # Petas
end
def GBToUnit (number)
@BYTE = 1024.0 * 1024.0 * 1024.0
@KILOBYTE = 1024.0 * 1024.0
@MEGABYTE = 1024.0
@TERABYTE = 1024.0
@PETABYTE = 1024.0 * 1024.0
@Bytes = (number * @BYTE).to_s
if number.to_s.length > 20 and @Bytes.length > 3 and @Bytes.split('.').first.length == 1
@Bytes = "Bytes too depth"
elsif @Bytes[0..2].eql? "0.0"
@Bytes = @Bytes.split('.').first + "." + @Bytes.split('.').last[0..1] + " Bytes"
elsif
@Bytes = @Bytes.split('.').first + "." + @Bytes.split('.').last[0] + " Bytes"
end # Bytes
@Kilos = (number * @KILOBYTE).to_s
if number.to_s.length > 20 and @Kilos.length > 3 and @Kilos.split('.').first.length == 1
@Kilos = "KB too depth"
elsif not @Kilos.include? "-" and not @Kilos[0..3].eql? "0.00"
@Kilos = @Kilos.split('.').first + "." + @Kilos.split('.').last[0..1] + " KB"
elsif
@Kilos = "0.0 KB".to_s
end # Kilos
@Megas = (number * @MEGABYTE).to_s
if number.to_s.length > 20 and @Megas.length > 3 and @Megas.split('.').first.length == 1
@Megas = "MB too depth"
elsif not @Megas.include? "-" and not @Megas[0..3].eql? "0.00"
@Megas = @Megas.split('.').first + "." + @Megas.split('.').last[0..1] + " MB"
elsif
@Megas = "0.0 MB".to_s
end # Megas
@Gigas = number.to_s + " GB"
@Teras = (number / @TERABYTE).to_s
if number.to_s.length > 20 and @Teras.length > 3 and @Teras.split('.').first.length == 1
@Teras = "TB too depth"
elsif not @Teras.include? "-"
@Teras = @Teras.split('.').first + "." + @Teras.split('.').last[0..1] + " TB"
elsif
@Teras = "0.0 TB".to_s
end # Teras
@Petas = (number / @PETABYTE).to_s
if number.to_s.length > 20 and @Petas.length > 3 and @Petas.split('.').first.length == 1
@Petas = "PB too depth"
elsif not @Petas.include? "-"
@Petas = @Petas.split('.').first + "." + @Petas.split('.').last[0..1] + " PB"
elsif
@Petas = "0.0 PB".to_s
end # Petas
end
def TBToUnit (number)
@BYTE = 1024.0 * 1024.0 * 1024.0 * 1024.0
@KILOBYTE = 1024.0 * 1024.0 * 1024.0
@MEGABYTE = 1024.0 * 1024.0
@GIGABYTE = 1024.0
@PETABYTE = 1024.0
@Bytes = (number * @BYTE).to_s
if number.to_s.length > 20 and @Bytes.length > 3 and @Bytes.split('.').first.length == 1
@Bytes = "Bytes too depth"
elsif @Bytes[0..2].eql? "0.0"
@Bytes = @Bytes.split('.').first + "." + @Bytes.split('.').last[0..1] + " Bytes"
elsif
@Bytes = @Bytes.split('.').first + "." + @Bytes.split('.').last[0] + " Bytes"
end # Bytes
@Kilos = (number * @KILOBYTE).to_s
if number.to_s.length > 20 and @Kilos.length > 3 and @Kilos.split('.').first.length == 1
@Kilos = "KB too depth"
elsif not @Kilos.include? "-" and not @Kilos[0..3].eql? "0.00"
@Kilos = @Kilos.split('.').first + "." + @Kilos.split('.').last[0..1] + " KB"
elsif
@Kilos = "0.0 KB".to_s
end # Kilos
@Megas = (number * @MEGABYTE).to_s
if number.to_s.length > 20 and @Megas.length > 3 and @Megas.split('.').first.length == 1
@Megas = "MB too depth"
elsif not @Megas.include? "-" and not @Megas[0..3].eql? "0.00"
@Megas = @Megas.split('.').first + "." + @Megas.split('.').last[0..1] + " MB"
elsif
@Megas = "0.0 MB".to_s
end # Megas
@Gigas = (number * @GIGABYTE).to_s
if number.to_s.length > 20 and @Gigas.length > 3 and @Gigas.split('.').first.length == 1
@Gigas = "GB too depth"
elsif not @Gigas.include? "-"
@Gigas = @Gigas.split('.').first + "." + @Gigas.split('.').last[0..1] + " GB"
elsif
@Gigas = "0.0 GB".to_s
end # Gigas
@Teras = number.to_s + " TB"
@Petas = (number / @PETABYTE).to_s
if number.to_s.length > 20 and @Petas.length > 3 and @Petas.split('.').first.length == 1
@Petas = "PB too depth"
elsif not @Petas.include? "-"
@Petas = @Petas.split('.').first + "." + @Petas.split('.').last[0..1] + " PB"
elsif
@Petas = "0.0 PB".to_s
end # Petas
end
def PBToUnit (number)
@BYTE = 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0
@KILOBYTE = 1024.0 * 1024.0 * 1024.0 * 1024.0
@MEGABYTE = 1024.0 * 1024.0 * 1024.0
@GIGABYTE = 1024.0 * 1024.0
@TERABYTE = 1024.0
@Bytes = (number * @BYTE).to_s
if number.to_s.length > 20 and @Bytes.length > 3 and @Bytes.split('.').first.length == 1
@Bytes = "Bytes too depth"
elsif @Bytes[0..2].eql? "0.0"
@Bytes = @Bytes.split('.').first + "." + @Bytes.split('.').last[0..1] + " Bytes"
elsif
@Bytes = @Bytes.split('.').first + "." + @Bytes.split('.').last[0] + " Bytes"
end # Bytes
@Kilos = (number * @KILOBYTE).to_s
if number.to_s.length > 20 and @Kilos.length > 3 and @Kilos.split('.').first.length == 1
@Kilos = "KB too depth"
elsif not @Kilos.include? "-" and not @Kilos[0..3].eql? "0.00"
@Kilos = @Kilos.split('.').first + "." + @Kilos.split('.').last[0..1] + " KB"
elsif
@Kilos = "0.0 KB".to_s
end # Kilos
@Megas = (number * @MEGABYTE).to_s
if number.to_s.length > 20 and @Megas.length > 3 and @Megas.split('.').first.length == 1
@Megas = "MB too depth"
elsif not @Megas.include? "-" and not @Megas[0..3].eql? "0.00"
@Megas = @Megas.split('.').first + "." + @Megas.split('.').last[0..1] + " MB"
elsif
@Megas = "0.0 MB".to_s
end # Megas
@Gigas = (number * @GIGABYTE).to_s
if number.to_s.length > 20 and @Gigas.length > 3 and @Gigas.split('.').first.length == 1
@Gigas = "GB too depth"
elsif not @Gigas.include? "-"
@Gigas = @Gigas.split('.').first + "." + @Gigas.split('.').last[0..1] + " GB"
elsif
@Gigas = "0.0 GB".to_s
end # Gigas
@Teras = (number * @TERABYTE).to_s
if number.to_s.length > 20 and @Teras.length > 3 and @Teras.split('.').first.length == 1
@Teras = "TB too depth"
elsif not @Teras.include? "-"
@Teras = @Teras.split('.').first + "." + @Teras.split('.').last[0..1] + " TB"
elsif
@Teras = "0.0 TB".to_s
end # Teras
@Petas = number.to_s + " PB"
end
# Error control
def args()
if ARGV.empty? or ARGV[0] == "/?" or ARGV[1] == nil or ARGV[2] == nil and not ARGV[1] == "bytes" and not ARGV[1] == "Bytes" and not ARGV[1] == "BYTES" and not ARGV[1] == "kb" and not ARGV[1] == "Kb" and not ARGV[1] == "KB" and not ARGV[1] == "kB" and not ARGV[1] == "mb" and not ARGV[1] == "Mb" and not ARGV[1] == "MB" and not ARGV[1] == "mB" and not ARGV[1] == "gb" and not ARGV[1] == "gB" and not ARGV[1] == "Gb" and not ARGV[1] == "GB" and not ARGV[1] == "tb" and not ARGV[1] == "Tb" and not ARGV[1] == "TB" and not ARGV[1] == "tB" and not ARGV[1] == "pb" and not ARGV[1] == "pB" and not ARGV[1] == "Pb" and not ARGV[1] == "PB"
help()
elsif not ARGV[2] == "bytes" and not ARGV[2] == "Bytes" and not ARGV[2] == "BYTES" and not ARGV[2] == "kb" and not ARGV[2] == "Kb" and not ARGV[2] == "KB" and not ARGV[2] == "kB" and not ARGV[2] == "mb" and not ARGV[2] == "Mb" and not ARGV[2] == "MB" and not ARGV[2] == "mB" and not ARGV[2] == "gb" and not ARGV[2] == "gB" and not ARGV[2] == "Gb" and not ARGV[2] == "GB" and not ARGV[2] == "tb" and not ARGV[2] == "Tb" and not ARGV[2] == "TB" and not ARGV[2] == "tB" and not ARGV[2] == "pb" and not ARGV[2] == "pB" and not ARGV[2] == "Pb" and not ARGV[2] == "PB"
$ToUnit = "all"
end
end
# Process
logo()
args()
input = ARGV[0]
unit = ARGV[1]
$ToUnit = ARGV[2]
input = input.gsub(',', '.')
if not input.length == 6 or not input.length == 5 and not input[-3] == "."
input = input.gsub('.', '')
input = input.to_i
elsif
input = input.to_f
end
if unit =~ /bytes/i
BytesToUnit(input)
elsif unit =~ /kb/i
KBToUnit(input)
elsif unit =~ /mb/i
MBToUnit(input)
elsif unit =~ /gb/i
GBToUnit(input)
elsif unit =~ /tb/i
TBToUnit(input)
elsif unit =~ /pb/i
PBToUnit(input)
end
if $ToUnit =~ /bytes/i
puts "#{input} #{unit} = #{@Bytes}"
elsif $ToUnit =~ /kb/i
puts "#{input} #{unit} = #{@Kilos}"
elsif $ToUnit =~ /mb/i
puts "#{input} #{unit} = #{@Megas}"
elsif $ToUnit =~ /gb/i
puts "#{input} #{unit} = #{@Gigas}"
elsif $ToUnit =~ /tb/i
puts "#{input} #{unit} = #{@Teras}"
elsif $ToUnit =~ /pb/i
puts "#{input} #{unit} = #{@Petas}"
elsif $ToUnit == nil
puts "#{input} #{unit} = #{@Bytes}"
puts "#{input} #{unit} = #{@Kilos}"
puts "#{input} #{unit} = #{@Megas}"
puts "#{input} #{unit} = #{@Gigas}"
puts "#{input} #{unit} = #{@Teras}"
puts "#{input} #{unit} = #{@Petas}"
end
__END__
No tengo ni idea de Batch, pero ¿que largos se hacen los codes no? :).
Buenta tool EleKtro a ver si me animo yo algún dia y la codeo yo en C o Python :)
No se Ruby, asi que mucho no me puedo meter pero.. no es demasiado largo? si solamente tiene que dividir o multiplicar tantas veces un numero por 1024. Tendrias que declarar una vez las variables que usaras y hacer 2 funciones, una para ver si no tiene errores y de paso acomodar lo que ingreso el usuario a tu gusto(si puso puntos o no..) y despues la otra funcion para multiplicar o dividir segun pida, con un bucle las veces que haya que hacerlo.
Se que aclaraste que lo hiciste asi porque total ya anda y listo, pero te digo para que lo mejores si queres. Saludos ;)
Cita de: $Edu$ en 31 Marzo 2012, 20:14 PM
no es demasiado largo?
Tendrias que declarar una vez las variables que usaras y hacer 2 funciones, una para ver si no tiene errores y de paso acomodar lo que ingreso el usuario a tu gusto(si puso puntos o no..) y despues la otra funcion para multiplicar o dividir segun pida, con un bucle las veces que haya que hacerlo.
Se que aclaraste que lo hiciste asi porque total ya anda y listo, pero te digo para que lo mejores si queres. Saludos ;)
Tienes toda la razón, Es largo xD, Voy dominando
Ruby poco a poco, Tengo que pensar bien en como puedo simplificar las funciones en una sola o dos como comentas.
Aunque, Creo que no lo haré si no me da por añádirle algo nuevo al code... Como dije, Así ya funca xD
Salu2!