Mega Code Archive

 
Categories / Delphi / Files
 

ShortFileName LongFileName

Title: ShortFileName / LongFileName Question: How to do this: 'PROGRA~1' - 'PROGRAM FILES' Answer: function ShortFileName (const FileName: string): string; var aTmp: array[0..255] of char; begin if not FileExists (FileName) then Result := '' else if GetShortPathName (PChar (FileName), aTmp, Sizeof (aTmp) - 1) = 0 then Result:= FileName else Result:= StrPas (aTmp); end; function LongFileName (ShortName: string): string; var SR: TSearchRec; begin Result := ''; if (pos ('\\', ShortName) + pos ('*', ShortName) + pos ('?', ShortName) 0) or not FileExists (ShortName) then { ignore NetBIOS name, joker chars and invalid file names } EXIT; while FindFirst (ShortName, faAnyFile, SR) = 0 do begin { next part as prefix } Result := '\' + SR.Name + Result; SysUtils.FindClose (SR); { the SysUtils, not the WinProcs procedure! } { directory up (cut before '\') } ShortName := ExtractFileDir (ShortName); if length (ShortName) Break; { ShortName contains drive letter followed by ':' } end; Result := ExtractFileDrive (ShortName) + Result; end;