# -*- Ruby -*- require 'win32ole' class WinShell # Get the path separator used by common ruby classes such as Dir and File. def separator() return File.const_get('SEPARATOR') end # My shell is the ole object "Shell" as described in the Windows SDK. def shell() # lazy initialisation, don't use @shell directly! @shell = WIN32OLE.new('Shell.application') if !@shell return @shell end # Has filename the Extension ext (e. g. ".jpg") def hasExtension?(filename, ext) raise ArgumentError, "Extension missing" if ext.empty? pExt = (ext[0] == '.'[0] ? ext : '.' + ext).downcase dcFilename = filename.downcase return File.basename(dcFilename, pExt) != dcFilename end # Is filename a Windows shortcut? def link?(filename) return hasExtension?(filename, '.lnk') end # Get a path string usable by Windows systems. def winPath(pathString) return pathString.gsub(separator, "\\") end # Get a path string usable by common ruby classes such as Dir and File. def rubyPath(pathString) return pathString.gsub(/\\/, separator) end # Get Directory entries as ole-objects, not as strings! def getFolderItems(directoryPathString) folder = shell.NameSpace(winPath(directoryPathString)) raise "Folderermittlung fehlgeschlagen" if !folder items = folder.items return (0..items.Count-1).collect { |i| items.item(i) } end #Get the path stored in the Windows shortcut filename. PRE: link?(filename) def getLinkTarget(filename, pathString = Dir.getwd) raise(ArgumentError, "Kein Link") if !link?(filename) ext = '.' + filename.split('.')[-1] linkdef = File.basename(filename, ext) raise ArgumentError, "Linkname leer" if linkdef.empty? item = getFolderItems(pathString).find { |fi| linkdef == (fi.Name.to_s) } return item ? rubyPath(item.GetLink.Path.to_s) : '' end end shell = WinShell.new ARGV.each { | arg | if shell.link?(arg) puts "#{arg} =>\n#{shell.getLinkTarget(arg)}" end }