2011-11-14 7 views
7

Quiero hacer una herramienta que toma algunos nombres de archivo como parámetros, pero cuando se utiliza este código:Cómo llegar nombre de archivo largo de ARGV

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

y lo uso de arrastrar y soltar o "Enviar a" en Windows , Obtengo el nombre de archivo corto. Así que un archivo como "C:\Ruby193\bin\test\New Text Document.txt" se convierte en C:\Ruby193\bin\test\NEWTEX~1.TXT como argumento.

No hay ningún problema cuando ejecuto el script desde la línea de comandos, con los nombres de archivos largos como parámetros.

¿Cómo obtengo el nombre de archivo largo cuando uso arrastrar y soltar o enviar?

Respuesta

4

No sé si es posible cambiar el argumento de que usted reciba en un arrastrar y soltar, pero se puede usar la función Win32 getLongPathName(), utilizando el Ruby Win32 bindings

--edit--
Incluyendo @ la solución de peter formateado para facilitar la lectura:

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

se encontró en http://www.varioustopics.com/ruby/518646-rre-ruby-cygwin-and-paths.html requieren 'encontrar' requerir '' fileutils requerir 'Win32API' def get_long_win32_filename (nombre corto) 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 | pone un puts get_long_win32_filename (a) final – 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

I aprendí mucho tratando de resolver esto!

Sin embargo, @peter me ganó con una solución mucho más simple.

Aquí está el mío, en caso de que alguien lo encuentre útil. file_get_long_name.rb

Obtuve la idea de: un artículo vb-world.net archivado y lo convirtió a 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

he encontrado la razón de que mi escritura receaved nombres de archivo cortos, que había hecho un parche de registro para permitir que el arrastrar y soltar en los guiones de rubí y schortcuts de la siguiente manera

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}" 

Pero tenía que ser el siguiente para nombres de archivo largos

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}" 
Cuestiones relacionadas