require 'pathname' require 'dl/win32' class Pathname unless class_variable_defined?('@@GetShortPathName') @@GetShortPathName = Win32API.new('Kernel32.dll', 'GetShortPathNameA', 'SSI', 'I') end def shortname olen = 200 begin buff = ' ' * olen len = @@GetShortPathName.call(relative? ? realpath.to_s : to_s, buff, buff.size) if olen < len olen = len end end while olen == len buff.rstrip.chomp("\0") end end if $0 == __FILE__ if ARGV.length == 0 $stderr.puts 'usage: winpath.rb pathname [more pathname ...]' exit 1 end ARGV.each do |f| p = Pathname.new(f) $stdout.puts p.shortname end end