John Smith
John Smith

Reputation: 6259

Iterate through folder and return full file path

I wrote this code:

verz = 'C:\Users\GerdPC\Desktop\mouth'

Dir.foreach(verz) do |f|
 next if f == '.' or f == '..' 
 puts f
end

My problem is that I don't know how to return not only the filename f for eg:

sumba.png

But:

C:\Users\GerdPC\Desktop\mouth\sumba.png

So I tried:

 puts File.expand_path(f)

But this only expands the filename with the directory in which my program is running! What should I do?

Upvotes: 1

Views: 1054

Answers (2)

Arup Rakshit
Arup Rakshit

Reputation: 118261

I would do it as below using File::realdirpath:

Dir.pwd # => "/home/kirti/Ruby"

Dir.foreach(Dir.pwd) do |file|
  puts File.realdirpath(file) unless ['.','..'].include? file
end

# >> /home/kirti/Ruby/tut.html
# >> /home/kirti/Ruby/test
#.........
#.........

Upvotes: 2

rohit89
rohit89

Reputation: 5773

Isn't this enough?

puts "#{verz}\\#{f}"

Upvotes: 2

Related Questions