Mega Code Archive

 
Categories / Delphi / Files
 

How to get the long path and file name of an existing file

Title: How to get the long path and file name of an existing file? Question: How to get the long path and file name of an existing file? Answer: // This function is useful, when you use ParamStr(1) to get a file name // from a file dropped on the icon of an application. // In this case ParamStr(1) returns the drive, path and file name // dropped, but in the DOS (8.3) format. // To convert the whole path and filename to a long path and file name // you can use this function. // // So: // C:\MYDOCU~1\TESTIN~1.DOC // will be converted by this function to: // C:\My Documents\Testing a tool.doc //---------------------------------------------------------------------- Function GetLongPathAndFilename(Const S : String) : String; Var srSRec : TSearchRec; iP,iRes : Integer; sTemp,sRest : String; Bo : Boolean; Begin Result := S + ' [directory not found]'; // Check if file exists Bo := FileExists(S); // Check if directory exists iRes := FindFirst(S + '\*.*',faAnyFile,srSRec); // If both not found then exit If ((not Bo) and (iRes 0)) then Exit; sRest := S; iP := Pos('\',sRest); If iP 0 then Begin sTemp := Copy(sRest,1,iP - 1); // Drive sRest := Copy(sRest,iP + 1,255); // Path and filename End else Exit; // Get long path name While Pos('\',sRest) 0 do begin iP := Pos('\',sRest); If iP 0 then Begin iRes := FindFirst(sTemp + '\' + Copy(sRest,1,iP - 1),faAnyFile,srSRec); sRest := Copy(sRest,iP + 1,255); If iRes = 0 then sTemp := sTemp + '\' + srSRec.FindData.cFileName; End; End; // Get long filename If FindFirst(sTemp + '\' + sRest,faAnyFile,srSRec) = 0 then Result := sTemp + '\' + srSRec.FindData.cFilename; SysUtils.FindClose(srSRec); End; //----------------------------------------------------------------------