DelphiFAQ Home Search:

Long file names - short file names

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Here's a way to convert between short (8.3 DOS file names) and long file names:

 {$apptype console}

 program LongShrt;

 uses
   Windows, SysUtils;

 function GetShortName(sLongName : string) : string;
 var
   sShortName    : string;
   nShortNameLen : integer;
 begin
   SetLength(sShortName, MAX_PATH);

   nShortNameLen := GetShortPathName(PChar(sLongName),
                      PChar(sShortName), MAX_PATH-1);

   if nShortNameLen = 0 then
   begin
     { handle errors... }
   end;

   SetLength(sShortName, nShortNameLen);

   Result := sShortName;
 end;


 function GetLongName(sShortName : string; var bError : boolean) : string;
 var
   bAddSlash : boolean;
   SearchRec : TSearchRec;
   nStrLen   : integer;
 begin
   bError    := False;
   Result    := sShortName;
   nStrLen   := Length(sShortName);
   bAddSlash := False;
   
   if sShortName[nStrLen] = '\' then
   begin
     bAddSlash := True;
     SetLength(sShortName, nStrLen-1);
     dec(nStrLen);
   end;
   
   if((nStrLen-Length(ExtractFileDrive(sShortName))) > 0) then
   begin
     if FindFirst(sShortName, faAnyFile, SearchRec) = 0 then
     begin
       Result := ExtractFilePath(sShortName) + SearchRec.name;
       if bAddSlash then
       begin
         Result := Result + '\';
       end;
     end
     else
     begin
       // handle errors...       bError := True;
     end;
     FindClose(SearchRec);
   end;
 end;

 function GetLongName(sShortName : string) : string;
 var
   s      : string;
   p      : integer;
   bError : boolean;
 begin
   Result := sShortName;
   
   s := '';
   p := Pos('\', sShortName);
   while(p > 0)do
   begin
     s := GetLongName(s + Copy(sShortName, 1, p), bError);
     Delete(sShortName, 1, p);
     p := Pos('\', sShortName);

     if(bError)then
       Exit;
   end;
   if sShortName <> '' then
   begin
     s := GetLongName(s + sShortName, bError);
     if bError then
       Exit;
   end;
   Result := s;
 end;


 const
   csTest = 'C:\program Files';

 var
   sShort,
   sLong   : string;

 begin
   sShort := GetShortName(csTest);
   WriteLn('Short name for "' + csTest +
     '" is "' + sShort + '"');

   WriteLn;
    
   sLong := GetLongName(sShort);
   WriteLn('Long name for "' + sShort + '" is "' + sLong + '"');
 end.


Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: