我无法从Ruby 1.9.2p290在Windows 7上复制具有Unicode字符命名的文件。
例如,在一个目录中有两个文件:
file
ハリー・ポッターと秘密の部屋
以下是代码:
(如果您看不到第二个名称中的日语字符)
> 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"
你看到我的 Ruby 文件系统编码是 "windows-1251",它是 8 位的,无法处理日语。将 default_external
和 default_internal
编码设置为 'utf-8' 也没有帮助。
我该如何从 Ruby 复制这些文件?
更新
我找到了一个解决方案。如果我使用 Dir.glob
或 Dir[]
而不是 Dir.entries,则会返回以 utf-8 编码的文件名,可以进行复制。
更新 #2
我的 Dir.glob
解决方案似乎相当有限。它仅适用于 "*" 参数:
Dir.glob("*") # <--- Shows Unicode names correctly
Dir.glob("c:/test/*") # <--- Does not work for Unicode names
#encoding: utf-8
或者以irb -Ku
的方式运行 irb 吗? - Frost