1. Computing
Shortcut target finder
A sample Delphi application, with full source code, that can be used to locate all the shortcut files (.LNK) on a drive (directory) and find the target that is executed by a shortcut.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• DOWNLOAD FULL SOURCE
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• How to create a .LNK shortcut
• How to create an Internet Shortcut (.URL) file
• Programming the Windows shell
• Finding files with Delphi
• Exploring Directories and Files

Original idea by Jim Boriau; GUI and text by Zarko Gajic.

File ... Properties ... Find Target ... :(
Windows shortcut is a special type of file linked to another object. We can think of a shortcut as a pointer to an object, it permits us to access a file or application without having to navigate to it on our hard drive.

Shortcuts always have a target associated with them which they invoke when executed. Depending on the target, the shortcut may open a folder, run a program, open a document, open a web page, and so on.

In order to manually locate the target file for a specified shortcut, you have to select the (shortcut) file, call the context menu (right click), pick Properties and select "Find Target" - this operation will open a Windows Explorer window with the shortcut's target selected in the located folder.

If you have an application where you want to allow users to *work* with shortcut files, you could add a code to display the standard Windows Properties dialog, but the "Find target" button needs to be selected manually.

Shortcut target finder :)
Here's a solution:

Shortcut target finder in action

The above "Shortcut target finder" is a *little dirty piece* of Delphi code that lists all the shortcuts in a given folder and locates the target items pointed by the shortcuts.

The "Analyze Structure" button locates all the .LNK (shortcut) files in a selected folder (and subfolders) and lists their names along with the targets in the Memo. The "Read target from LNK" button simply displays a message indicating the target of the selected shortcut. The code uses the ideas from the "Finding files with Delphi" article to locate all the .LNK files on a drive/directory.

You can download the full source code. If you interested only in the "Find target" part of the code, here it is:

uses ... ShlObj, ActiveX;
...
function TForm1.GetTarget
  (const LinkFileName:String):String;
var
   psl  : IShellLink;
   ppf  : IPersistFile;
   WidePath  : Array[0..260] of WideChar;
   Info      : Array[0..MAX_PATH] of Char;
   wfs       : TWin32FindData;
begin
 if UpperCase(ExtractFileExt(LinkFileName)) <> '.LNK' Then
 begin
   Result:='NOT a shortuct by extension!';
   Exit;
 end;

 CoCreateInstance(CLSID_ShellLink, 
                  nil, 
                  CLSCTX_INPROC_SERVER, 
                  IShellLink, 
                  psl);
 if psl.QueryInterface(IPersistFile, ppf) = 0 then
 begin
   MultiByteToWideChar(CP_ACP,
                       MB_PRECOMPOSED, 
                       PChar(LinkFileName), 
                       -1, 
                       @WidePath, 
                       MAX_PATH);
   ppf.Load(WidePath, STGM_READ);
   psl.GetPath(@info, 
               MAX_PATH, 
               wfs, 
               SLGP_UNCPRIORITY);
   Result := info;
 end
 else
   Result := '';
end;

I just love those handy pieces of Delphi / API code. Here's an idea ... create a program that locates all the shortcuts that point to all your programs on a client machine.

©2013 About.com. All rights reserved.