• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

Abbreviated/shortened file paths (c:\program files\borl...\bde.exe)

Does anyone have any code that creates an abbreviated/truncated string representing a file or folder path like:

c:\program files\borl...\bde.exe

There are times when you want to display a TLabel or TString that displays the full path to a file.  However, sometimes the path is so long that it spills off the form.  I've seen other applications abbreviate long file paths by inserting dots ... in the middle of the path.

Anyone have code that does this?

Thanks.
0
Softtech
Asked:
Softtech
  • 2
1 Solution
 
JohnjcesCommented:
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<&gt0 then Inc(Index);
      if (Index=Num)and(Num<&gt0) 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)&gtCount)and(Num&gt2) 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;


From Greatis.com

John
0
 
SofttechAuthor Commented:
Thanks, but the code is badly flawed.

The problem with the code is that it inserts \...\ when it needs not.  For instance,...

GetShortPath('c:\windows\win.ini',64);

...will return the crazy:

c:\windows\...\...\...\...\...\win.ini

Then at other times it totally inserts nothing.  E.g.

GetShortPath('D:\ZIP Code Access\2000Version\MO Test\disk9.mdb',32)

Even though 'D:\ZIP Code Access\2000Version\MO Test\disk9.mdb' is 49 characters, the function inserts no \...\ at all.

0
 
JohnjcesCommented:
I had no problems or strange things after I cleaned up the copied code. Sorry about that.

Try this...
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;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now