Title: Get absolute Path from Relative Path
Question: how Get absolute Path from Relative Path;
examples:
F:\jq\myprogram\Yl\Yl\Yl', '..\..\..\vc\delphi\BAK\vcp.exe'
Vcp.exe path is \jq\myprogram\vc\delphi\BAK\
Answer:
function GetappearNum(sub, st: string): integer;
var
i: integer;
P: integer;
begin
p := Pos(sub, st);
I := 0;
while p 0 do
begin
inc(i);
delete(st, 1, p + length(sub) - 1);
p := Pos(sub, st);
end;
result := i;
end;
function decomposestr(sub, st: string; var tst: TStringList): Boolean;
var
num: integer;
P: integer;
begin
p := Pos(sub, st);
tst.Clear;
while p 0 do
begin
num := p + length(sub) - 1;
tst.Add(copy(st, 1, num));
delete(st, 1, num);
p := Pos(sub, st);
end;
tst.Add(st);
Result := True;
end;
function CopyLeftNum(sub, st: string; num: integer): string;
var
Tst: TStringList;
I: integer;
begin
tst := TStringList.Create;
decomposestr(sub, st, Tst);
if Num = Tst.Count then
Result := st
else
begin
for i := 0 to num - 1 do
begin
Result := Result + Tst[i];
end;
end;
Tst.Free;
end;
function CopyRightNum(sub, st: string; Num: integer): string;
var
Tst: TStringList;
I: integer;
begin
Tst := TStringList.Create;
try
decomposestr(sub, st, Tst);
Result := '';
if Num begin
for i := Tst.Count - Num to Tst.Count - 1 do
begin
Result := Result + Tst[i]
end;
end;
finally
Tst.Free;
end;
end;
function GetabsolutePath(Source, Relative: string): string;
var
i, Num, num1: integer;
St: TStringList;
s: string;
begin
Num := GetappearNum('..', Relative);
St := TStringList.Create;
decomposestr('\', Source, st);
Num1 := st.Count;
Result := '';
for i := 0 to num1 - num - 1 do
begin
Result := Result + st[i];
end;
s := CopyRightNum('..\', Relative, 1);
Result := Result + s;
st.Free;
end;
Run:
GetabsolutePath(F:\jq\myprogram\Yl\Yl\Yl', '..\..\..\vc\delphi\BAK\vcp.exe');
E_mail:yanleigis@21cn.com