How to get long filename from ARGV

后端 未结 4 1419
野性不改
野性不改 2020-12-18 02:59

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

and

4条回答
  •  被撕碎了的回忆
    2020-12-18 03:56

    I learned a lot trying to figure this out!

    However, @peter beat me to it with a much simpler solution.

    Here is mine, in case someone finds it useful. file_get_long_name.rb

    I got the idea from: an archived vb-world.net article and converted it to 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"
    

提交回复
热议问题