1. Computing & Technology

Discuss in my forum

Code a Custom ExtractBasePath Delphi Function

By , About.com Guide

See More About:
Question: Code a Custom ExtractBasePath Delphi Function
Challenge: a custom Delphi function, ExtractBasePath, should take 2 path names (directory or file) and should return the base / common path for the two paths provided.

The code is submitted for the ExtractBasePath Delphi Challenge

Answer: ExtractBasePath entry by Ivan Cvetkovic:
 uses Math; 

 function ExtractBasePath(const path1, path2 : string) : string;
 procedure SplitPath(Path: string; sl: TStrings) ;
 begin
 sl.Delimiter := PathDelim;
 sl.StrictDelimiter := True;
 sl.DelimitedText := Path;
 end;
 var
 sl1, sl2: TStrings;
 cnt: Integer;
 begin
 Result := EmptyStr;
 
 sl1 := TStringList.Create;
 try
 SplitPath(Path1, sl1) ;
 
 sl2 := TStringList.Create;
 try
 SplitPath(Path2, sl2) ;
 
 for cnt := 0 to Min(sl1.Count, sl2.count) - 1 do
 begin
 if not AnsiSameText(sl1[cnt], sl2[cnt]) then Break;
 Result := Result + sl1[cnt] + PathDelim;
 end;
 finally
 sl2.Free;
 end;
 finally
 sl1.Free;
 end;
 end;
 
Explore the list of all accepted entries for ExtractBasePath Delphi challenge.

Readers Respond: Your Delphi Programming Challenges

©2012 About.com. All rights reserved.

A part of The New York Times Company.