ruby - 将长文件名转换为短文件名 (8.3)

标签 ruby windows command-line long-filenames short-filenames

我想将长文件名/路径转换为短文件名 (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/

相关文章:

ruby-on-rails - 序列化数组的条件验证

检查之前由应用程序启动的进程是否始终返回事件进程

c - 作为父进程运行子进程

c - 如何检查命令行参数中的最后一个字符?

mysql - 如何使用 MySQL 中的命令行获取用户帐户列表?

linux - 将命令更改为不 fork 和耗尽资源的命令

ruby-on-rails - 使用 Capistrano 3 设置默认阶段

ruby - 当引发异常并且程序停止执行时,哪个系统信号被发送到 ruby​​ 程序?

javascript - 如何让 Guard 自动编译我的 Coffeescript 文件?说是但不是

c# - 如何在 Windows Phone 8.1 中清除导航缓存模式的缓存