• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 934
  • Last Modified:

Shortcuts and their paths

How to get target path of a shortcut in Start menu, on Desktop, etc.? How to process next shortcut after checking path of previous shortcut?
And how to write paths in Memo?
Delphi 3
Step by step, please
0
pipigril
Asked:
pipigril
1 Solution
 
LischkeCommented:
Hi pipigril,

you asked actually three questions. This is not fair and against the rules at E-E. Only one question at a time.

1) For the task of getting the path from a shortcut:

function FileNameFromLink(const LinkName: String): String;

// dereferences the given link name to the file/folder name associated with the link

var
  AObject: IUnknown;
  ASLink: IShellLink;
  APFile: IPersistFile;
  WFileName: WideString;
  PFD: TWin32FindData;

begin
  // create a helper interface to dereference the link (because this is local it will
  // be freed automatically on exit)
  AObject := CreateComObject(CLSID_ShellLink);
  ASLink := AObject as IShellLink;
  APFile := AObject as IPersistFile;
  // convert to wide string
  WFileName := LinkName;
  APFile.Load(PWideChar(WFileName), 0);
  SetLength(Result, MAX_PATH);
  ASLink.GetPath(PChar(Result), MAX_PATH, PFD, 0);
end;


2) For enumerating shortcuts just use FindFirst/FindNext as with any other file.

3) Adding the path to a memo is the easiest part:

  Mmeo1.Lines.Add(NewPath);

Ciao, Mike
0
 
pipigrilAuthor Commented:
But.... How to use FindNext etc.? I am beginner :-).
0
 
LischkeCommented:
procedure EnumerateFiles(const Folder: String; EnumProc: TGetStrProc);

var
  SR: TSearchRec;

begin
  if FindFirst(Folder + '*.lnk', faReadOnly or faHidden, SR) = 0 then
  begin
    repeat
      EnumProc(Folder + SR.Name);
    until FindNext(SR) <> 0;
    FindClose(SR);
  end;
end;

procedure TMainForm.Enum(const S: String);

begin
  Memo1.Lines.Add(S);
end;

procedure TMainForm.Button2Click(Sender: TObject);

begin
  EnumerateFiles('C:\WINNT4\Profiles\All Users\Startmen├╝\Programme\Microsoft Visual Studio 6.0\Microsoft Visual Studio 6.0 Tools\', Enum);
end;


Ciao, Mike
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
rwilson032697Commented:
listening
0
 
yk030299Commented:
listen
0
 
MadshiCommented:
Mike, just one addition: In your "FileNameFromLink" function you should add this line at the end of the function:

result := pchar(result);

This will cause Delphi to cut the string at the first #0 character. If you leave it the way it is right now, the result string will always have a length of MAX_PATH. That's quite ugly...

Regards, Madshi.
0
 
LischkeCommented:
Good point Madshi. Thank you :-)

Ciao, Mike
0
 
pipigrilAuthor Commented:
Hmmm....... good. Can somebody write a small sample to me? (I don't know how to complet your code-parts. There are many ways...)
0
 
LischkeCommented:
There's already code for complete enumeration. What do you want more? See my comment from Wednesday, February 02 2000 - 08:26AM PST. All you need now is to create a form place a button and a memo on it, place the EnumerateFiles call in the click handler (give it a path you actually have on your machine). Copy the other code I gave you there you go..

Ciao, Mike
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now