2

Is there a built-in method in Ruby to convert a Windows short path like

C:\PROGRA~2\MICROS~1.0\VC\bin\amd64

into its corresponding long path

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64

I've tried

  • File.expand_path(short_path)
  • Pathname.new(short_path).cleanpath
  • Pathname.new(short_path).realpath
  • Dir.chdir(short_path) { Dir.pwd }

but none of these worked.

If possible, I'd like to avoid calling the Win32 API directly like in this answer, or ugly work-arounds like spawning PowerShell:

%x{powershell (Get-Item -LiteralPath #{short_path}).FullName}
Community
  • 1
  • 1
sschuberth
  • 28,386
  • 6
  • 101
  • 146

1 Answers1

3

Here is one possible workaround:

path=Dir.mktmpdir('vendor')
=> "C:/Users/ADMINI~1/AppData/Local/Temp/1/vendor20160727-12668-ywfjol"

Dir.glob(path)[0]
=> "C:/Users/Administrator/AppData/Local/Temp/1/vendor20160727-12668-ywfjol"
kit.yang
  • 2,758
  • 3
  • 17
  • 17