unit LFN_ALT;
interface
// This unit provides two functions that convert
// filenames from the long format to the 8.3
// format, and from the 8.3 format to the long
// format. It also contains two functions that
// convert pathnames to and from the long and
// 8.3 formats.
// This unit originally appeared in Delphi Developer's
// Journal, the August, 1996 issue. To see the article
// online, go to:
// [ http://www.cobb.com/ddj/9608/ddj9681.htm ]
function
AlternateToLFN(alternateName: String):
String;
function
LFNToAlternate(LongName: String):
String;
function
AlternateToLongPath(alternateName: String):
String;
function
LongPathToAlternate(LongName: String):
String;
implementation
uses Windows, SysUtils;
function
AlternateToLFN(alternateName: String):
String;
var temp: TWIN32FindData;
searchHandle: THandle;
begin
searchHandle :=
FindFirstFile(PChar(alternateName), temp);
if searchHandle <> ERROR_INVALID_HANDLE then
begin
result := String(temp.cFileName);
if result = `' then
result := String(temp.cAlternateFileName);
end
else
result := `';
Windows.FindClose(searchHandle);
end;
function
LFNToAlternate(LongName: String):
String;
var temp: TWIN32FindData;
searchHandle: THandle;
begin
searchHandle :=
FindFirstFile(PChar(LongName), temp);
if searchHandle <> ERROR_INVALID_HANDLE then
begin
result := String(temp.cAlternateFileName);
if result = `' then
result := String(temp.cFileName);
end
else
result := `';
Windows.FindClose(searchHandle);
end;
function
AlternateToLongPath(alternateName: String):
String;
var lastSlash: PChar;
tempPathPtr: PChar;
begin
result := `';
tempPathPtr := PChar(alternateName);
lastSlash := StrRScan(tempPathPtr, `\');
while lastSlash <> nil do
begin
result := `\' + AlternateToLFN(tempPathPtr) +
result;
if lastSlash <> nil then
begin
lastSlash^ := char(0);
lastSlash := StrRScan(tempPathPtr, `\');
end
end;
result := tempPathPtr + result;
end;
function
LongPathToAlternate(LongName: String):
String;
var lastSlash: PChar;
tempPathPtr: PChar;
begin
result := `';
tempPathPtr := PChar(LongName);
lastSlash := StrRScan(tempPathPtr, `\');
while lastSlash <> nil do
begin
result := `\' + LFNToAlternate(tempPathPtr) +
result;
if lastSlash <> nil then
begin
lastSlash^ := char(0);
lastSlash := StrRScan(tempPathPtr, `\');
end;
end;
result := tempPathPtr + result;
end;
end.
--
Tim Gooch
Editor-in-Chief,
Delphi Developer's Journal
[ http://www.cobb.com/ddj ]