SPECIAL OFFER        Order and save right now!
20% off with the 729824315 dicscount code for Ultimate Pack and any another product for Delphi from Greatis Programming!

⤷ Get short path to the file

You may use GetShortPath function. Path and Count are input parameters. Path - is a long path to the file (for example, 'c:\Program Files\AAA\BBB\CCC\DDD\fff.txt'). GetShortPath function returns a short path (for example, 'c:\Program Files\...\DDD\fff.txt') and Count parameter is a limit of this short path. Length of short path will be less, than "Count" symbols

function GetShortPath(Path: string; Count: Integer): string;

  procedure Slashes(var Str: string; var Num: Integer);
  var
    Position, Index: Integer;
  begin
    Index:=0;
    repeat
      Position:=Pos('\', Str);
      Delete(Str,1,Position);
      if Position<>0 then Inc(Index);
      if (Index=Num)and(Num<>0) then break;
    until Position=0;
    Num:=Index;
  end;

var
  Num, NewNum, P: Integer;
  Str: string;
begin
  Str:=Path;
  Num:=0;
  Slashes(Path, Num);
  while (Length(Str)>Count)and(Num>2) do
  begin
    NewNum:=Num div 2;
    Path:=Str;
    Slashes(Path, NewNum);
    P:=Pos(Path, Str);
    Delete(Str,P, Length(Path));
    NewNum:=2;
    Slashes(Path, NewNum);
    Str:=Str+'...\'+Path;
    Dec(Num);
  end;
  Result:=Str;
end;
Related components
TFileSearch
TFolderMonitor
TFileVersion
More for developers
Databases for Amazon Associates
Amazon Categories
Amazon Nodes