将长文件名转换为短文件名(8.3) [英] Convert long filename to short filename (8.3)

查看:335
本文介绍了将长文件名转换为短文件名(8.3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将长文件名/路径转换为短文件名(8.3). 我正在开发一个脚本,该脚本调用仅接受短文件名的命令行工具.

I want to convert long filenames/path to short filenames (8.3). I'm developing a script that calls a command line tool that only accepts short filenames.

所以我需要转换

C:\Ruby193\bin\test\New Text Document.txt

C:\Ruby193\bin\test\NEWTEX~1.TXT

到目前为止,我发现了如何从ARGV获取长文件名使用WIN32API将短文件名转换为长文件名(与我想要实现的相反).

So far i found How to get long filename from ARGV which uses WIN32API to convert short to long filenames (the opposite of what I want to achieve).

有没有办法在Ruby中获取短文件名?

Is there any way to get the short filename in Ruby?

推荐答案

此ruby代码使用 getShortPathName ,不需要安装其他模块.

This ruby code uses getShortPathName and don't need additional modules to be installed.

def get_short_win32_filename(long_name)
    require 'win32api'
    win_func = Win32API.new("kernel32","GetShortPathName","PPL"," L")
    buf = 0.chr * 256
    buf[0..long_name.length-1] = long_name
    win_func.call(long_name, buf, buf.length)
    return buf.split(0.chr).first
end

这篇关于将长文件名转换为短文件名(8.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆