Solved

How to get file name and path from a link file?

Posted on 2001-08-11
10
201 Views
Last Modified: 2010-09-07
I need to get the file name and path from a link file, does anyone knows how it can be done?
0
Comment
Question by:Yevgen
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 1

Expert Comment

by:Gani2001
ID: 6375629
Hi there,

There are many file naming functions in Delphi

ExtractFileName return file name with out path

ExtractFilePath return file path

or u can use TSearchRec to get file name

FindFirst(Str1+'\*.*',faAnyFile,F)

you can get file name like this F.Name

Abdelghani
0
 
LVL 1

Expert Comment

by:Gani2001
ID: 6375638
Hi there,

There are many file naming functions in Delphi

ExtractFileName return file name with out path

ExtractFilePath return file path

or u can use TSearchRec to get file name

FindFirst(Str1+'\*.*',faAnyFile,F)

you can get file name like this F.Name

Abdelghani
0
 

Author Comment

by:Yevgen
ID: 6375983
Gani2001,
What you suggested is to find the path and filename of the link(.lnk) file but what i need is to find the file to wich the link file is attached...
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 17

Expert Comment

by:inthe
ID: 6376078
this is example from the paq's,hope is some help:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,shlobj,activex,comobj,commctrl,menus;

type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }

  public
    { Public declarations }
  end;

var
  Form1: TForm1;
   NeedToUninitialize : boolean = false;
implementation

{$R *.DFM}

function LoadShellLink(shellLinkFile: string;
                       var description, linkedObj, params: string;
                       var pidl: PItemIDList; var findData: TWin32FindData;
                       var iconPath: string; var iconIndex: integer;
                       var workingDir, hotKey: string;
                       var showCmd: integer) : boolean;
var sl  : IShellLink;
    w1  : word;
    s1  : string;
    wfd : TWin32FindData;
    i1  : integer;
begin
  NeedToUninitialize:=NeedToUninitialize or succeeded(CoInitialize(nil));
  sl:=CreateComObject(CLSID_ShellLink) as IShellLink;
  result:=(sl as IPersistFile).Load(PWideChar(wideString(ShellLinkFile)),0)=S_OK;
  if not result then exit;
  SetLength(description,MAX_PATH+1);
  if sl.GetDescription(PChar(description),MAX_PATH)=NOERROR then description:=string(PChar(description))
  else                                                           description:='';
  SetLength(s1,MAX_PATH+1);
  if sl.GetPath(PChar(s1),MAX_PATH,wfd,0)=NOERROR then begin
    linkedObj:=string(PChar(s1));
    findData :=wfd;
  end else begin
    linkedObj:='';
    ZeroMemory(@findData,sizeOf(TWin32FindData));
  end;
  SetLength(params,MAX_PATH+1);
  if sl.GetArguments(PChar(params),MAX_PATH)=NOERROR then params:=string(PChar(params))
  else                                                    params:='';
  if sl.GetIDList(pidl)<>NOERROR then pidl:=nil;
  SetLength(s1,MAX_PATH+1);
  if sl.GetIconLocation(pchar(s1),MAX_PATH,i1)=NOERROR then begin
    iconPath :=string(pchar(s1));
    iconIndex:=i1;
  end else begin
    iconPath :='';
    iconIndex:=-1;
  end;
  SetLength(workingDir,MAX_PATH+1);
  if sl.GetWorkingDirectory(PChar(workingDir),MAX_PATH)=NOERROR then workingDir:=string(PChar(workingDir))
  else                                                               workingDir:='';
  sl.GetHotKey(w1);
  if w1 and (HOTKEYF_ALT     shl 8)<>0 then w1:=(w1 and (not (HOTKEYF_ALT     shl 8))) or scAlt;
  if w1 and (HOTKEYF_CONTROL shl 8)<>0 then w1:=(w1 and (not (HOTKEYF_CONTROL shl 8))) or scCtrl;
  if w1 and (HOTKEYF_SHIFT   shl 8)<>0 then w1:=(w1 and (not (HOTKEYF_SHIFT   shl 8))) or scShift;
  hotKey:=ShortCutToText(w1);
  if sl.GetShowCmd(showCmd)<>NOERROR then showCmd:=-1;
end;

procedure FreePidl(var pidl: PItemIDList);
var malloc : IMalloc;
begin
  if (pidl<>nil) and (SHGetMalloc(malloc)=NOERROR) then begin
    malloc.Free(pidl);
    pidl:=nil;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  pi:PItemIDList;
  s1,Desc,s2:String;
  icon,workdir,htkey:string;
  Data: TWin32FindData;
  i,ii : integer;
begin
  opendialog1.execute;
  loadshelllink(opendialog1.filename,desc,s1,s2,pi,Data,icon,i,workdir,htkey,ii);
 //opendialog1.filename now contains the correct file path.
 showmessage(opendialog1.filename);
//for other possible infos
 {
  ShowMessage('File ShortCut : '+opendialog1.filename+#13#10+'File TruePath : '+s1+#13#10+'Working Dir : '+workdir+#13#10
              +'HotKey : '+htkey+#13#10+'Show Command : '+inttostr(ii));
  }
 FreePidl(pi);
end;


initialization
finalization
  if NeedToUninitialize then CoUninitialize;

end.
0
 

Author Comment

by:Yevgen
ID: 6376099
Inthe,

can you please explain me wehre exactly this function gets the file path and name and what is going on there?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6376110
You have to call LoadShellLink('c:\path\example.lnk') and give in some local variables. The variable "linkedObj" will tell you the path and name of the linked object.

Regards, Madshi.
0
 
LVL 17

Expert Comment

by:inthe
ID: 6376147
the .lnk file is loaded into a ipersistfile and read by a ishelllink which we create.
when dealing with CreateComObject we need the initialize coinitialize ..stuff so better not delete that
from there all the stuff is read like filename,description,shortcut keys etc).
sl.getnamepath is the part your most interested in,as it gets you your path.
you may probably want to delete all the extra stuff if you want the file path only.

heres the smaller example that only gets the path not all the other stuff:


uses shlobj,activex,comobj;



var
 Form1: TForm1;
  NeedToUninitialize : boolean = false;
implementation

{$R *.DFM}

function LoadShellLink(shellLinkFile : string;
                    findData: TWin32FindData ) : boolean;
var sl  : IShellLink;
   s1  : string;
   wfd : TWin32FindData;
begin
 NeedToUninitialize:=NeedToUninitialize or succeeded(CoInitialize(nil));
 sl:=CreateComObject(CLSID_ShellLink) as IShellLink;
 result:=(sl as IPersistFile).Load(PWideChar(wideString(ShellLinkFile)),0)=S_OK;
 if not result then exit;
 sl.GetPath(PChar(s1),MAX_PATH,wfd,0);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 s1:string;
 Data: TWin32FindData;
begin
 opendialog1.execute;
 loadshelllink(opendialog1.filename,Data);
 showmessage(opendialog1.filename);
end;

initialization
finalization
 if NeedToUninitialize then CoUninitialize;

end.
0
 
LVL 17

Expert Comment

by:inthe
ID: 6376154
totally ignore last comment ,ive pasted from wrong delphi window

oh and i should note that opendialg does it for you (well does for me on win2k) so showmessage(opendialog1.filename) will already show the linked file.
0
 
LVL 17

Accepted Solution

by:
inthe earned 100 total points
ID: 6376180
not alot of errror cheking :-< but short version could be like:

uses comobj,activex,shlobj;

procedure TForm1.Button1Click(Sender: TObject);
var
IU          :IUnknown;
PersistFile :IPersistFile;
iSL         :IShellLink;
wfd         :TWin32FindDataA;
linkedObj   :PChar;
i           :Integer;
LNKFile     :WideString;
begin
  i:=300;
  GetMem(linkedObj,i);
  IU:=CreateComObject(CLSID_ShellLink);
  iSL:= IU as IShellLink;
  PersistFile:= IU as IPersistFile;
  LNKFile:='C:\test.lnk';
  PersistFile.Load(PWChar(LNKFile),1);
  iSL.GetPath(linkedObj,i, wfd, SLGP_UNCPRIORITY);
  ShowMessage(StrPas(linkedObj));
  FreeMem(linkedObj);
 end;
0
 

Author Comment

by:Yevgen
ID: 6376293
Thanks Inthe, i used your code and it works great...
Oh, before you posted the comment about not deleting the CoInitialize i did delete it and it still worked...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 79
How to debug For loops? 3 57
can't find the executable in Simulator 1 100
Delphi and Access based Enumeration 9 70
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question