Created
November 16, 2011 02:11
-
-
Save zemoxian/1369060 to your computer and use it in GitHub Desktop.
Get a long path from a short path on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# idea from :http://web.archive.org/web/20020617214130/http://vb-world.net/files/tip511.html | |
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I created this to try an answer a Stack Overflow question. Someone found a much simpler solution, however...