Yevgen
asked on
How to get file name and path from a link file?
I need to get the file name and path from a link file, does anyone knows how it can be done?
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+'\*.*',faAn yFile,F)
you can get file name like this F.Name
Abdelghani
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+'\*.*',faAn
you can get file name like this F.Name
Abdelghani
ASKER
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...
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...
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,co mobj,commc trl,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(shellLinkFil e: 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:=NeedTo Uninitiali ze or succeeded(CoInitialize(nil ));
sl:=CreateComObject(CLSID_ ShellLink) as IShellLink;
result:=(sl as IPersistFile).Load(PWideCh ar(wideStr ing(ShellL inkFile)), 0)=S_OK;
if not result then exit;
SetLength(description,MAX_ PATH+1);
if sl.GetDescription(PChar(de scription) ,MAX_PATH) =NOERROR then description:=string(PChar( descriptio n))
else description:='';
SetLength(s1,MAX_PATH+1);
if sl.GetPath(PChar(s1),MAX_P ATH,wfd,0) =NOERROR then begin
linkedObj:=string(PChar(s1 ));
findData :=wfd;
end else begin
linkedObj:='';
ZeroMemory(@findData,sizeO f(TWin32Fi ndData));
end;
SetLength(params,MAX_PATH+ 1);
if sl.GetArguments(PChar(para ms),MAX_PA TH)=NOERRO R then params:=string(PChar(param s))
else params:='';
if sl.GetIDList(pidl)<>NOERRO R then pidl:=nil;
SetLength(s1,MAX_PATH+1);
if sl.GetIconLocation(pchar(s 1),MAX_PAT H,i1)=NOER ROR then begin
iconPath :=string(pchar(s1));
iconIndex:=i1;
end else begin
iconPath :='';
iconIndex:=-1;
end;
SetLength(workingDir,MAX_P ATH+1);
if sl.GetWorkingDirectory(PCh ar(working Dir),MAX_P ATH)=NOERR OR then workingDir:=string(PChar(w orkingDir) )
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)<>NO ERROR then showCmd:=-1;
end;
procedure FreePidl(var pidl: PItemIDList);
var malloc : IMalloc;
begin
if (pidl<>nil) and (SHGetMalloc(malloc)=NOERR OR) 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,d esc,s1,s2, pi,Data,ic on,i,workd ir,htkey,i i);
//opendialog1.filename now contains the correct file path.
showmessage(opendialog1.fi lename);
//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.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,shlobj,activex,co
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(shellLinkFil
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:=NeedTo
sl:=CreateComObject(CLSID_
result:=(sl as IPersistFile).Load(PWideCh
if not result then exit;
SetLength(description,MAX_
if sl.GetDescription(PChar(de
else description:='';
SetLength(s1,MAX_PATH+1);
if sl.GetPath(PChar(s1),MAX_P
linkedObj:=string(PChar(s1
findData :=wfd;
end else begin
linkedObj:='';
ZeroMemory(@findData,sizeO
end;
SetLength(params,MAX_PATH+
if sl.GetArguments(PChar(para
else params:='';
if sl.GetIDList(pidl)<>NOERRO
SetLength(s1,MAX_PATH+1);
if sl.GetIconLocation(pchar(s
iconPath :=string(pchar(s1));
iconIndex:=i1;
end else begin
iconPath :='';
iconIndex:=-1;
end;
SetLength(workingDir,MAX_P
if sl.GetWorkingDirectory(PCh
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)<>NO
end;
procedure FreePidl(var pidl: PItemIDList);
var malloc : IMalloc;
begin
if (pidl<>nil) and (SHGetMalloc(malloc)=NOERR
malloc.Free(pidl);
pidl:=nil;
end;
end;
procedure TForm1.Button1Click(Sender
var
pi:PItemIDList;
s1,Desc,s2:String;
icon,workdir,htkey:string;
Data: TWin32FindData;
i,ii : integer;
begin
opendialog1.execute;
loadshelllink(opendialog1.
//opendialog1.filename now contains the correct file path.
showmessage(opendialog1.fi
//for other possible infos
{
ShowMessage('File ShortCut : '+opendialog1.filename+#13
+'HotKey : '+htkey+#13#10+'Show Command : '+inttostr(ii));
}
FreePidl(pi);
end;
initialization
finalization
if NeedToUninitialize then CoUninitialize;
end.
ASKER
Inthe,
can you please explain me wehre exactly this function gets the file path and name and what is going on there?
can you please explain me wehre exactly this function gets the file path and name and what is going on there?
You have to call LoadShellLink('c:\path\exa mple.lnk') and give in some local variables. The variable "linkedObj" will tell you the path and name of the linked object.
Regards, Madshi.
Regards, Madshi.
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,short cut 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(shellLinkFil e : string;
findData: TWin32FindData ) : boolean;
var sl : IShellLink;
s1 : string;
wfd : TWin32FindData;
begin
NeedToUninitialize:=NeedTo Uninitiali ze or succeeded(CoInitialize(nil ));
sl:=CreateComObject(CLSID_ ShellLink) as IShellLink;
result:=(sl as IPersistFile).Load(PWideCh ar(wideStr ing(ShellL inkFile)), 0)=S_OK;
if not result then exit;
sl.GetPath(PChar(s1),MAX_P ATH,wfd,0) ;
end;
procedure TForm1.Button1Click(Sender : TObject);
var
s1:string;
Data: TWin32FindData;
begin
opendialog1.execute;
loadshelllink(opendialog1. filename,D ata);
showmessage(opendialog1.fi lename);
end;
initialization
finalization
if NeedToUninitialize then CoUninitialize;
end.
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,short
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(shellLinkFil
findData: TWin32FindData ) : boolean;
var sl : IShellLink;
s1 : string;
wfd : TWin32FindData;
begin
NeedToUninitialize:=NeedTo
sl:=CreateComObject(CLSID_
result:=(sl as IPersistFile).Load(PWideCh
if not result then exit;
sl.GetPath(PChar(s1),MAX_P
end;
procedure TForm1.Button1Click(Sender
var
s1:string;
Data: TWin32FindData;
begin
opendialog1.execute;
loadshelllink(opendialog1.
showmessage(opendialog1.fi
end;
initialization
finalization
if NeedToUninitialize then CoUninitialize;
end.
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.fi lename) will already show the linked file.
oh and i should note that opendialg does it for you (well does for me on win2k) so showmessage(opendialog1.fi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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...
Oh, before you posted the comment about not deleting the CoInitialize i did delete it and it still worked...
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+'\*.*',faAn
you can get file name like this F.Name
Abdelghani