2

I want to get all filenames in UTF-8. For example, after I read filename in Windows, I do

filename = Iconv.iconv("UTF-8", "Windows-1251", filename)

In Ubuntu I don't convert filename and get it in UTF-8. Maybe exists some method to determine OS filename encoding?

Mikhail K.
  • 567
  • 1
  • 6
  • 15
  • 3
    What version of Ruby are you using? (1.8.x versus 1.9.x makes a big difference when talking about encoding and string handling) – Phrogz Mar 30 '11 at 17:20
  • Currently I use Ruby 1.8, but I can move on Ruby 1.9. So my question actually for both versions. But I don't work with Ruby 1.9. – Mikhail K. Mar 31 '11 at 04:23
  • 1
    Be careful, most OS support various encoding for their FS. For example, Windows does support UTF-16 ( cf http://stackoverflow.com/questions/2050973/what-encoding-are-filenames-in-ntfs-stored-as ) – Oct Apr 26 '11 at 22:08

1 Answers1

0

I do this on Ruby 1.9 when I want to make sure things are in UTF-8:

if filename.encoding.to_s != 'UTF-8'
  filename.encode!('UTF-8')
end

Unless your OS gives a file name encoded with a coding system which doesn't support some special characters found on the file name, it can be encoded to UTF-8 without a hickup.

dimitarvp
  • 2,316
  • 2
  • 20
  • 29