delivarator
delivarator

Reputation: 373

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?

Upvotes: 10

Views: 2624

Answers (3)

gix
gix

Reputation: 5797

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.

Upvotes: 4

Evgenii
Evgenii

Reputation: 37329

My solution was to use Dir.glob instead of Dir.entries. But it only works with * parameter. It does not work when passing a path (c:/dir/*). Tested in 1.9.2p290 and 1.9.3p0 on Windows 7.

There are many other issues with unicode paths on Windows. It is still an open issue. The patches are currently targeted at Ruby 2.0, which is rumored to be released in 2013.

Upvotes: 0

gmags
gmags

Reputation: 101

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

Upvotes: 10

Related Questions