Mega Code Archive

 
Categories / Delphi / Files
 

How to get the short name of the path name

Title: How to get the short name of the path name Question: How to get the short name of the path name ? Example: c:\program files\common files\microsoft shared in DOS format is: C:\PROGRA~1\COMMON~1\MICROS~1 Answer: function ShortDir(Dir: String): String; var SearchRec : TSearchRec; Ok : Integer; Ind : Integer; Name : String; TmpDir : String; DirDst : String; begin if Copy(Dir, Length(Dir), 1) = '\' then Dir := Copy(Dir, 1, Length(Dir)-1); TmpDir := Dir; while Length(TmpDir) 3 do begin Ok := FindFirst(TmpDir, faDirectory, SearchRec); FindClose(SearchRec); if Ok = 0 then begin if SearchRec.FindData.cAlternateFileName[0] = #0 then begin Name := SearchRec.Name; end else begin Ind := 0; Name := ''; while Ind if (SearchRec.FindData.cAlternateFileName[Ind] = #0) then Ind := 14 else Name := Name + SearchRec.FindData.cAlternateFileName[Ind]; Inc(Ind); end; end; DirDst := Name + '\' + DirDst; end; TmpDir := ExtractFileDir(TmpDir); end; DirDst := TmpDir + DirDst; if Copy(DirDst, Length(DirDst), 1) = '\' then DirDst := Copy(DirDst, 1, Length(DirDst)-1); Result := DirDst; end;