{******************************************************************************}
{ }
{ Delphi-Unit DotPath V1.0 }
{ }
{ Copyright (c) 2001 Michael Puff }
{ [url]www.luckie.de.vu[/url] }
{ [email]michaelpuff74@compuserve.de[/email] }
{ }
{******************************************************************************}
{ What it does...
Diese Unit verkürzt einen lange Dateipfad zu einem kurzen mit Pünktchen,
wobei man die Anzahl der Backslashes angeben kann die von vorne bzw. von hinten
angezeigt werden sollen
Beispiel:
macht aus:
E:\Michael\eigene Dateien\Programmierung\Delphi\Programme\VCL\in Bearbeitung\MichaelsMusikDB02.zip
mit den Einstellungen 2 Backslashes von vorne und 2 von hinten das:
E:\Michael\...\in Bearbeitung\MichaelsMusikDB02.zip
How to use this garbage of code...
Einfach die Unit in die Uses-Klausel aufnehmen und
DotPath.ShortPath(Path, 2, 2);
ergibt obiges Ergebnis, wenn Path der oben genannte Beispiel-Pfad ist
}
unit DotPath;
interface
function ShortPath(Path:
String; NumSlashesBegin, NumSlashesEnd: Byte):
String;
function CountBackSlashes(Path:
String): Integer;
implementation
uses windows;
{ für so ein bißchen keine SysUtils }
function ExtractFileName(
const FileName:
string):
string;
var
RPos: Integer;
begin
RPos := Length(FileName);
while not (FileName[RPos]
in ['
\', '
:'])
and (RPos > 0)
do
Dec(RPos);
Result := Copy(FileName, RPos + 1, MaxInt);
end;
function ExtractFilePath(
const FileName:
string):
string;
var
RPos: Integer;
begin
RPos := Length(FileName);
while not (FileName[RPos]
in ['
\', '
:'])
and (RPos > 0)
do
Dec(RPos);
Result := Copy(FileName, 0, RPos + 1);
end;
function CountBackSlashes(Path:
String): Integer;
var
i: byte;
cBackSlash: Byte;
{ Anzahl der BackSlashes }
begin
{ Anzahl der BackSlashes ermitteln }
cBackSlash := 0;
for i := 1
to length(Path)
do
begin
if Path[i] = '
\'
then
Inc(cBackSlash);
end;
result := cBackSlash;
end;
function ShortPath(Path:
String; NumSlashesBegin, NumSlashesEnd: Byte):
String;
{ Path: na was wohl ;))
{ NumSlashes: Anzal der "\"-Dinger, die angezeigt werde sollen bevor die
Pünktchens kommen }
var
i, j: byte;
aBackSlash:
array of Byte;
{ Array für die BackSlashes und deren Position }
ShortPath:
String;
{ gekürzter Pfad }
Temp:
String;
FileName:
String;
begin
{ Nur mal kucken, ob da keiner Blödsinn eingibt }
if (NumSlashesBegin > CountBackSlashes(Path))
or
(NumSlashesEnd > CountBackSlashes(Path))
then
begin
result := Path;
exit;
end;
if (NumSlashesEnd > CountBackSlashes(Path))
or
(NumSlashesEnd > CountBackSlashes(Path))
then
begin
result := Path;
exit;
end;
{ Eingaben korrigieren }
if CountBackSlashes(Path) <= 3
then
begin
result := Path;
exit;
end;
if NumSlashesBegin = 0
then NumSlashesBegin := 1;
if NumSlashesEnd = 0
then NumSlashesEnd := 1;
{ Größe des BackSlash-Arrays setzen }
SetLength(aBackSlash, CountBackSlashes(Path));
{ BackSlash-Array mit den Positionen der BackSlashes füllen }
j := 0;
for i := 0
to length(Path)
do
begin
if Path[i] = '
\'
then
begin
aBackSlash[j] := i;
Inc(j);
end;
end;
ShortPath := copy(Path, 0, aBackSlash[NumSlashesBegin-1]-1);
Temp := copy(Path, abackSlash[CountBackSlashes(Path)-NumSlashesEnd],
length(ExtractFilePath(Path)));
FileName := ExtractFileName(Path);
result := ShortPath + '
\...' + Temp;
end;
end.