Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
940 views
in Technique[技术] by (71.8m points)

ruby - How to get long filename from 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

and I use drag and drop or "send to" in Windows, I get the short filename. So a file like "C:Ruby193inestNew Text Document.txt" becomes C:Ruby193inestNEWTEX~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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...