How to copy files with Unicode characters to file names in Ruby?

I cannot copy files with Unicode characters in their names from Ruby 1.9.2p290 on Windows 7.

For example, I have two files in a directory:

file
ハリー・ポッターと秘密の部屋

(The middle name contains Japanese characters if you do not see it)

Here is the code:

> entries = Dir.entries(path) - %w{ . .. }
> entries[0]
=> "file"
> entries[1]
=> "???????????????" # <--- what?

> File.file? entries[0]
=> true
> File.file? entries[1]
=> false   # <---  !!! Ruby can not see it and will not copy

> entries[1].encoding.name
=> "Windows-1251"
> Encoding.find('filesystem').name
=> "Windows-1251"

As you can see, my Ruby file system encoding is “windows-1251”, which has 8 bits and cannot handle Japanese. Setting default_externaland default_internalencodings to "utf-8" does not help.

How to copy these files from Ruby?

Update

I have found a solution. It works if I use Dir.globor Dir[]instead of Dir.entries. File names are now returned in utf-8 encoding and can be copied.

Update # 2

My Dir.glob . "*":

Dir.glob("*") # <--- Shows Unicode names correctly
Dir.glob("c:/test/*") # <--- Does not work for Unicode names
+5
2

, , :

Dir.glob("*") # <--- Shows Unicode names correctly
Dir.glob("c:/test/*") # <--- Does not work for Unicode names

, :

Dir.chdir("c:/test/")
Dir.glob("*")

?

0

- , , , .

, Dir#entries Ruby> = 2.1.

Dir.entries(path, encoding: Encoding::UTF_8)
0

All Articles