Board index » delphi » Shorten long file path with "..."?

Shorten long file path with "..."?

Hi all,

I thought there was a function to shorten long file paths
with a "...". That is, in many apps, if there is a
path being displayed on screen that is too long to fit in
the display field, the app will shorten the path.
For example:

c:\program files\microsoft office\pictures\jpeg

might become:

C:\program files\...\jpeg

Am I worng? Is there a function in D5 to shorten a file
path like the above? TIA.

 - Dave

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----

 

Re:Shorten long file path with "..."?


In article <3ae859ce$...@news5.newsfeeds.com>, "Dave" <fornals...@mediaone.net>
writes:

Quote
>Am I worng? Is there a function in D5 to shorten a file
>path like the above? TIA.

I have only D3 and don't know. But there _is_ a windows API call to do it,
encapsulated into a function as follows ...

function PathEllipsis(Path : string; Control : TWinControl;
                      Rect : TRect): string;
begin
  SetLength(Path, Length(Path));   // forces a copy of APath string
  DrawTextEx(TCanvasCC(Control).Canvas.Handle, PChar(Path), -1,
             Rect, DT_PATH_ELLIPSIS or DT_MODIFYSTRING, nil);
  Result := Path;
end;

... and used as ...

      with TitleLbl do
        Caption := PathEllipsis(FileName, ContOuterPnl,
                                Rect(Left, Top, Left + Width, Top + Height)) ;

... when the file name was in a label on a TPanel.

You could use DT_WORD_ELLIPSIS and get truncation with ellipses at the end of
the string instead of in the path. But that would not be of much use, methinks
<g>.

Alan Lloyd
alangll...@aol.com

Re:Shorten long file path with "..."?


Or you can use the MinimizeName() function.   =P

MinimizeName(YOUR PATH HERE, Canvas, Length);

Example:

In my app, I use: MinimizeName('C:\Program Files\ICQ\ICQ.EXE',
StatusBar1.Canvas, 155);

That gives me: C:\..\ICQ\ICQ.EXE

Only problem with MinimizeName is that it NEEDS a canvas to work with.
So, unless you are using something other than a statusbar or something,
Alans solution is better.

Ryan

On 26 Apr 2001 21:19:24 GMT, alangll...@aol.com (AlanGLLoyd) wrote:

Quote
>In article <3ae859ce$...@news5.newsfeeds.com>, "Dave" <fornals...@mediaone.net>
>writes:

>>Am I worng? Is there a function in D5 to shorten a file
>>path like the above? TIA.

>I have only D3 and don't know. But there _is_ a windows API call to do it,
>encapsulated into a function as follows ...

>function PathEllipsis(Path : string; Control : TWinControl;
>                      Rect : TRect): string;
>begin
>  SetLength(Path, Length(Path));   // forces a copy of APath string
>  DrawTextEx(TCanvasCC(Control).Canvas.Handle, PChar(Path), -1,
>             Rect, DT_PATH_ELLIPSIS or DT_MODIFYSTRING, nil);
>  Result := Path;
>end;

>... and used as ...

>      with TitleLbl do
>        Caption := PathEllipsis(FileName, ContOuterPnl,
>                                Rect(Left, Top, Left + Width, Top + Height)) ;

>... when the file name was in a label on a TPanel.

>You could use DT_WORD_ELLIPSIS and get truncation with ellipses at the end of
>the string instead of in the path. But that would not be of much use, methinks
><g>.

>Alan Lloyd
>alangll...@aol.com

Re:Shorten long file path with "..."?


In article <sm5het8oaj2favj90k4nbvt6cp96ed7...@4ax.com>, Ryan Styles

Quote
<ner...@d.cat> writes:
>In my app, I use: MinimizeName('C:\Program Files\ICQ\ICQ.EXE',
>StatusBar1.Canvas, 155);

>That gives me: C:\..\ICQ\ICQ.EXE

>Only problem with MinimizeName is that it NEEDS a canvas to work with.
>So, unless you are using something other than a statusbar or something,
>Alans solution is better.

Not necessarily, any solution must have a Canvas in order to use
Canvas.TextWidth to establish the space taken by the text. My solution asks for
a control and I use it's Canvas.

MinimizeName (which I did not know of) ellipses at the start of the path, while
DrawText with DT_PATH_ELLIPSIS ellipses at the end of the path. I think that
MinimizeName (for all its slightly crummy code) is a better GUI solution. One
is usually more interested in a file's parent directory than the oririginal
folder on the drive.

Alan Lloyd
alangll...@aol.com

Re:Shorten long file path with "..."?


On 27 Apr 2001 07:01:19 GMT, alangll...@aol.com (AlanGLLoyd) wrote:

Quote
>Not necessarily, any solution must have a Canvas in order to use
>Canvas.TextWidth to establish the space taken by the text. My solution asks for
>a control and I use it's Canvas.

Ahh, I did not see that. I skimmed over your code example real fast and
didn't see any canvas reference. Was in a rush.  =P

Quote
>MinimizeName (which I did not know of) ellipses at the start of the path, while
>DrawText with DT_PATH_ELLIPSIS ellipses at the end of the path. I think that
>MinimizeName (for all its slightly crummy code) is a better GUI solution. One
>is usually more interested in a file's parent directory than the oririginal
>folder on the drive.

Wow, I actually showed somebody something for once? Hehe, now I feel
semi-useful!

But why do you feel it is a better GUI solution?

Cheers!
Ryan

Re:Shorten long file path with "..."?


In article <94tjetk9ts1bnf7t14cepb3rfohvu17...@4ax.com>, Ryan Styles

Quote
<ner...@d.cat> writes:
>But why do you feel it is a better GUI solution?

Because (as I said) for ...

E:\AProject\LexaCom\My Stuff\Archive\Priority.exe

... I think it would be more useful to me to see ...

E:\...\Archive\Priority.exe

... than ...

E:\AProject\...\Priority.exe

But it may depend on the particular file and application.

Alan Lloyd
alangll...@aol.com

Re:Shorten long file path with "..."?


Thi is a bad solution

Quote
> E:\AProject\LexaCom\My Stuff\Archive\Priority.exe

> ... I think it would be more useful to me to see ...

> E:\...\Archive\Priority.exe

One must be able to see as many directories closest to root (\), including
the last pathname, like tihs :

E:\AProject\LexaCom\...\Archive\Priority.exe

You have no chance of getting a hint of where the file is located when using

Quote
> E:\...\Archive\Priority.exe

The other

E:\AProject\LexaCom\...\Archive\Priority.exe

Gives you a hint of where the file is located 2 subdirs deep, thereby
reducing your search.
if there is not enough room for this, one takes as many dirs closest to root
as possible... like this

E:\AProject\...\Priority.exe

if this is too long.... expand your label ;)

--
Regards
Ingolf

Quote
"AlanGLLoyd" <alangll...@aol.com> wrote in message

news:20010428060426.06030.00000330@nso-co.aol.com...
Quote
> In article <94tjetk9ts1bnf7t14cepb3rfohvu17...@4ax.com>, Ryan Styles
> <ner...@d.cat> writes:

> >But why do you feel it is a better GUI solution?

> Because (as I said) for ...

> E:\AProject\LexaCom\My Stuff\Archive\Priority.exe

> ... I think it would be more useful to me to see ...

> E:\...\Archive\Priority.exe

> ... than ...

> E:\AProject\...\Priority.exe

> But it may depend on the particular file and application.

> Alan Lloyd
> alangll...@aol.com

Re:Shorten long file path with "..."?


"Ingolf" <DONT_SPAM_ing...@musling.dk> skrev i melding
news:w9RG6.65817$o4.5852960@news010.worldonline.dk...

Quote
> "AlanGLLoyd" <alangll...@aol.com> wrote in message
> news:20010428060426.06030.00000330@nso-co.aol.com...

> Thi is a bad solution

> > E:\AProject\LexaCom\My Stuff\Archive\Priority.exe

> > ... I think it would be more useful to me to see ...

> > E:\...\Archive\Priority.exe

> One must be able to see as many directories closest to root (\), including
> the last pathname, like tihs :

> E:\AProject\LexaCom\...\Archive\Priority.exe

> You have no chance of getting a hint of where the file is located when
using
> > E:\...\Archive\Priority.exe

> The other

> E:\AProject\LexaCom\...\Archive\Priority.exe

> Gives you a hint of where the file is located 2 subdirs deep, thereby
> reducing your search.
> if there is not enough room for this, one takes as many dirs closest to
root
> as possible... like this

> E:\AProject\...\Priority.exe

What about

  E:\APr..\Lex..\My ..\Arc..\Priority.exe

- the worst thing about shortening paths is that the most common error made -
choosing wrong directory level - is not revealed with compressed paths done
the usual way. An error that I often do (e.g. when I fail to notice that the
.zip archive contains fully qualified paths) ends up in copying or unzipping
files like this:

E:\AProject\LexaCom\My Stuff\Archive\LexaCom\My Stuff\Archive\Priority.exe

..then it doesn't help much to see either the first two or the last two
directory levels...

--
Bjoerge Saether
Consultant / Developer
http://www.itte.no
Asker, Norway
bjorgeremovet...@itte.no (remove the obvious)

Re:Shorten long file path with "..."?


Quote
>>MinimizeName(YOUR PATH HERE, Canvas, Length);<<

What unit is this in? D5 Doesn't seem to know what
to do with it? Or it it an API I need to declare?

 - Dave

Other Threads