2011-11-14 61 views
7

獲得長文件名我想要一個工具,它需要一些文件名作爲參數,但是當我使用此代碼:如何從ARGV

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

,我使用拖放或「發送到」在Windows ,我得到了短文件名。 因此像"C:\Ruby193\bin\test\New Text Document.txt"這樣的文件變成 C:\Ruby193\bin\test\NEWTEX~1.TXT作爲參數。

當我使用longfilenames作爲參數從命令行運行腳本時,沒有任何問題。

當我使用拖放或發送時,如何獲得長文件名?

回答

4

我不知道是否有可能改變你在拖拽收到的參數,但你可以使用Win32 getLongPathName()功能,採用Ruby Win32 bindings

- 編輯 -
包括@彼得的解決方案格式化的可讀性:

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 
+0

沒有找到它 http://www.varioustopics.com/ruby/518646-rre-ruby-cygwin-and-paths.html 需要 '查找' 需要 'fileutils中' 需要 '的Win32API' DEF get_long_win32_filename(SHORT_NAME) MAX_PATH = 1024 LONG_NAME = 「」 * MAX_PATH lfn_size = Win32API.new( 「KERNEL32」, 「GetLongPathName」,[」 (short name,long_name,max_path) return(1..max_path).include?(lfn_size)? long_name [0..lfn_size-1]:短名稱 結束 ARGV.each do | a | 放一個 puts get_long_win32_filename(a) end – peter

5

http://www.varioustopics.com/ruby/518646-rre-ruby-cygwin-and-paths.html

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 
0

我學習了很多,試圖弄清楚這一點!

然而@peter用一個簡單得多的解決方案打敗了我。

這是我的,以防有人發現它有用。 file_get_long_name.rb

我的想法來自:歸檔的vb-world.net文章,並將其轉換爲ruby。

require 'win32ole' 

def get_long_filename(shortpath, fso = WIN32OLE.new("Scripting.FileSystemObject")) 
    path = case 
    when fso.FolderExists(shortpath) 
    fso.GetFolder(fso.GetAbsolutePathName(shortpath)) 
    when fso.FileExists(shortpath) 
    fso.GetFile(fso.GetAbsolutePathName(shortpath)) 
    else 
    return nil 
    end 
    parts = path.Path.split(/\\/) 

    working = fso.GetDrive(parts.shift).RootFolder 
    longpath = working.Path 
    parts.each do |part| 
    temppath = fso.BuildPath(longpath, part) 
    working = fso.GetFolder(longpath) 
    if fso.FolderExists(temppath) 
     working.SubFolders.each do |sub| 
     longpath = fso.BuildPath(longpath, sub.Name) if part== sub.ShortName || part == sub.Name 
     end 
    elsif fso.FileExists(temppath) 
     working.Files.each do |sub| 
     longpath = fso.BuildPath(longpath, sub.Name) if part== sub.ShortName || part == sub.Name 
     end 
    end 
    end 
    longpath 
end 


fso = WIN32OLE.new("Scripting.FileSystemObject") 
short = "C:\\DOCUME~1\\jamal\\Desktop\\NEWTEX~1.TXT" 
long = get_long_filename(short, fso) 
p long 
# ==> "C:\\Documents and Settings\\jamal\\Desktop\\New Text Document.txt" 
0

,我發現我的劇本receaved短文件名的原因,我做了一個註冊表補丁,使阻力和Ruby腳本和schortcuts下降如下

Windows Registry Editor Version 5.00 

[HKEY_CLASSES_ROOT\rbfile\ShellEx\DropHandler] 
@="{86C86720-42A0-1069-A2E8-08002B30309D}" 

[HKEY_CLASSES_ROOT\rbwfile\ShellEx\DropHandler] 
@="{86C86720-42A0-1069-A2E8-08002B30309D}" 

[HKEY_CLASSES_ROOT\RubyFile\ShellEx\DropHandler] 
@="{86C86720-42A0-1069-A2E8-08002B30309D}" 

[HKEY_CLASSES_ROOT\RubyWFile\ShellEx\DropHandler] 
@="{86C86720-42A0-1069-A2E8-08002B30309D}" 

但它必須是以下長文件名

Windows Registry Editor Version 5.00 

[HKEY_CLASSES_ROOT\rbfile\ShellEx\DropHandler] 
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}" 

[HKEY_CLASSES_ROOT\rbwfile\ShellEx\DropHandler] 
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}" 

[HKEY_CLASSES_ROOT\RubyFile\ShellEx\DropHandler] 
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}" 

[HKEY_CLASSES_ROOT\RubyWFile\ShellEx\DropHandler] 
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"