Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode filenames on Windows in Ruby

I have a piece of code that looks like this:

Dir.new(path).each do |entry|
    puts entry
end

The problem comes when I have a file named こんにちは世界.txt in the directory that I list. On a Windows 7 machine I get the output:

???????.txt

From googling around, properly reading this filename on windows seems to be an impossible task. Any suggestions?

like image 684
delivarator Avatar asked Apr 24 '10 04:04

delivarator


2 Answers

I had the same problem & just figured out how to get the entries of a directory in UTF-8 in Windows. The following worked for me (using Ruby 1.9.2p136):

opts = {}
opts[:encoding] = "UTF-8"
entries = Dir.entries(path, opts)
entries.each do |entry|
  # example
  stat = File::stat(entry)
  puts "Size: " + String(stat.size)
end
like image 104
gmags Avatar answered Oct 26 '22 10:10

gmags


You're out of luck with pure ruby (either 1.8 or 1.9.1) since it uses the ANSI versions of the Windows API.

It seems like Ruby 1.9.2 will support Unicode filenames on Windows. This bug report has 1.9.2 as target. According to this announcement Ruby 1.9.2 will be released at the end of July 2010.

If you really need it earlier you could try to use FindFirstFileW etc. directly via Win32API.new or win32-api.

like image 43
gix Avatar answered Oct 26 '22 12:10

gix