我想将长文件名/路径转换为短文件名 (8.3)。 我正在开发一个调用只接受短文件名的命令行工具的脚本。
所以我需要转换
C:\Ruby193\bin\test\New Text Document.txt
到
C:\Ruby193\bin\test\NEWTEX~1.TXT
到目前为止我找到了How to get long filename from ARGV它使用 WIN32API 将短文件名转换为长文件名(与我想要实现的相反)。
有没有办法在 Ruby 中获取短文件名?
最佳答案
您可以使用 FFI 执行此操作;在 their wiki 中实际上有一个示例涵盖了您的确切场景在“将路径转换为 8.3 样式路径名”标题下:
require 'ffi'
module Win
extend FFI::Library
ffi_lib 'kernel32'
ffi_convention :stdcall
attach_function :path_to_8_3, :GetShortPathNameA, [:pointer, :pointer, :uint], :uint
end
out = FFI::MemoryPointer.new 256 # bytes
Win.path_to_8_3("c:\\program files", out, out.length)
p out.get_string # be careful, the path/file you convert to 8.3 must exist or this will be empty
关于ruby - 将长文件名转换为短文件名 (8.3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10224572/