Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Shortened pathname with \...\

Posted on 1998-09-03
6
496 Views
Last Modified: 2010-08-05
How do I shorten pathnames to fit a certain length space using an ellipsis (...) between backslashes?
0
Comment
Question by:rclay
6 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1338552
How about something similar to this.,.....

var
  Path : string;
procedure TForm1.Button1Click(Sender: TObject);
begin
  if Length(path) > 10 then
    Path := Copy(path, 1, 10) + '\...\';
  Caption := Path;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
  Path := 'C:\windows\desktop\Viktor\Delphi';
  Caption := Path;
end;

Hope it helps! There are other ways to do that with the API, but it's not exactly what you are looking for     it does like this  C:\windows\desktop\vik...  and not C:\windows\desktop\vik\...\...\and so on.....

Regards,
Viktor Ivanov
0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1338553
If you want to do it the "Windows Way", let me know and I'll show you how to do it..
0
 

Author Comment

by:rclay
ID: 1338554
Viktornet: What I'm looking for is a function or procedure that shortens the path by omitting its middle part, substituting an ellipsis. But it would break the pathname at a '\', not at some arbitrary point such as at the 10th character. Also the total length of the resulting pathname should be adjustable at design- or run-time by the programmer.

Blackman: Yes, I would like to know how to do it the windows way.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:JimBob091197
ID: 1338555
Hi

The DrawTextEx API function will fill in the ellipsis for you.  You give the function a string (PChar), a rectangle and a canvas, and if the text doesn't fit into the rectangle then it uses ellipsis.

E.g.
DrawTextEx(Canvas.Handle, PChar(SomeStr), Length(SomeStr), SomeRect, DT_PATH_ELLIPSIS or DT_MODIFYSTRING, nil);

Unfortunately you don't get the result string (with ellipsis) back.

JB
0
 
LVL 7

Accepted Solution

by:
BlackMan earned 100 total points
ID: 1338556
Here's an example (taken from a "live" application). It takes the value of AString and shorten it so that it fits in Label1.

Var
  AString: String;
  r: TRect;
begin
  AString := '\\MyServer\MyShare\ThisDirectory\ThatDirectory\MyFile';
  r.Left := 0;
  r.Right := Label1.Width;
  r.Top := 0;
  r.Bottom := Label1.Height;
  Windows.DrawText(Label1.Canvas.Handle, @AString[1], Length(AString), r, DT_Left or DT_PATH_ELLIPSIS or DT_MODIFYSTRING or DT_CALCRECT );
  Label1.caption := AString;
End;
0
 

Author Comment

by:rclay
ID: 1338557
Thanks for the input Viktornet, JimBob, and BlackMan!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question