Dan-Levi Tømta Dan-Levi Tømta - 6 months ago 10x
Ruby Question

Why does this ruby script fail to execute?

I have this ruby script that is supposed to copy/rename a set of images, but it wont work for some reason. My experience with ruby is little so i wonder why it wont work.

Here is the script.

tile_width = 256
tile_height = 256
image_width = 1024
image_height = 6144
n = 0
# To get this number, look at the number of tiles
# generated, find the last tile number and add 1
# e.g. tiles_99.png => total_tiles = 100
total_tiles = 256

tiles_per_column = image_width/tile_width

row = 0
column = 0
(n...total_tiles).each do |i|
filename = "tiles_#{i}.png" # current filename
target = "map_#{column}_#{row}.png" # new filename

puts "copy #{filename} to #{target}"

`cp -f #{filename} #{target}` # rename

# work out next step
column = column + 1
if column >= tiles_per_column
column = 0
row = row + 1

And here is the error which is returned

copy tiles_0.png to map_0_0.png
script.rb:21:in ``': No such file or directory - cp -f tiles_0.png map_0_0.png (Errno::ENOENT)
from script.rb:21:in `block in <main>'
from script.rb:15:in `each'
from script.rb:15:in `<main>'

And here is the images i am trying to rename/copy

Screenshot of image folder


I'll assume you are running on windows so no cp command for you, use FileUtils::cp instead