如何从 ARGV 获取长文件名 [英] How to get long filename from ARGV

查看:36
本文介绍了如何从 ARGV 获取长文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个将一些文件名作为参数的工具,但是当我使用此代码时:

I want to make a tool that takes some filenames as parameters, but when I use this code:

ARGV.each do|a|
  puts "Argument: #{a}"
end

我在 Windows 中使用拖放或发送到",我得到了短文件名.所以像 "C:Ruby193in estNew Text Document.txt" 这样的文件就变成了C:Ruby193in estNEWTEX~1.TXT 作为参数.

and I use drag and drop or "send to" in Windows, I get the short filename. So a file like "C:Ruby193in estNew Text Document.txt" becomes C:Ruby193in estNEWTEX~1.TXT as the argument.

当我从命令行运行脚本时没有问题,使用长文件名作为参数.

There is no problem when I run the script from the commandline, with the longfilenames as parameters.

使用拖放或发送到时如何获取长文件名?

How do i get the long filename when i use drag and drop or send to?

推荐答案

我不知道是否可以更改您在拖放时收到的参数,但您可以使用 Win32 getLongPathName()a> 函数,使用 Ruby Win32 绑定

I don't know if it is possible to change the argument you recieve on a drag and drop, but you could use the Win32 getLongPathName() function, using the Ruby Win32 bindings

--编辑--
包括@peter 为便于阅读而格式化的解决方案:

--edit--
Including @peter's solution formatted for readability:

require 'find'
require 'fileutils'
require 'Win32API'
def get_long_win32_filename(short_name)
  max_path = 1024
  long_name = " " * max_path
  lfn_size = Win32API.new("kernel32", 
      "GetLongPathName", ['P','P','L'],'L').call(short_name, long_name, max_path)
  return (1..max_path).include?(lfn_size) ? long_name[0..lfn_size-1] : short_name
end 

ARGV.each do|a|
  puts a
  puts get_long_win32_filename(a)
end

这篇关于如何从 ARGV 获取长文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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