File.rename doesnt work in a loop

Hello,

the following line works outside of the loop but not within the loop.
Please help. what am I doing wrong?
Thank you,
Meir

output = ls
for file in output do
File.rename(file,"#{file}".chomp+".d")
end

When I do

output = ls

I get one string with all the filenames. I believe you want them
individually. This can be done with #split.

files = ls.split("\n")
files.each do |file|
File.rename(file, “#{file.chomp}.d”)
end

It worked. Thanks so much :wink:

Mads Ohm L. wrote in post #1167081:

files = ls.split("\n")
files.each do |file|
File.rename(file, “#{file.chomp}.d”)
end

I’m sorry, but it is ridiculous to use “ls” to obtain a list of
filenames. This can be done much more efficient and reliable by Ruby
itself:

files = Dir.entries(’.’)
files = Dir[’’]
files = Dir[’
’, ‘.*’] # all, including dot files

require ‘pathname’
files = Pathname(’.’).children

Cheers

robert

Robert K. wrote in post #1167096:

Mads Ohm L. wrote in post #1167081:

Cheers

robert

I am not getting any of the forum posts via mailing list. Are you facing
the same issue? Just confirming if mine is not working or it is
happening for all. Are you answering this posts via RubyForum web link ?

Robert K. wrote in post #1167096:

I’m sorry, but it is ridiculous to use “ls” to obtain a list of
filenames.

You are totally correct. I only used ls as that was the path that Meir
had already chosen.