» Help

Code Snippets Database

Control Box
 
 

Requested snippet

This page displays the requested ShortToLongFilePath snippet.

You can choose a category, perform a search or generate a Pascal unit containing the snippet by using the Control Box.

ShortToLongFilePath
Converts whole of given DOS style 8.3 path to long file path and returns it. If path can't be converted then '' is returned.
function ShortToLongFilePath(const FilePath: string): string;
var
  PrevPath: string;         // path before last file/dir in FilePath
  ExpandedName: string;     // long form of file name
  SR: SysUtils.TSearchRec;  // record used by file find functions
  Success: Integer;         // indicates success in finding a file
  // ---------------------------------------------------------------------------
  function CountPathDelims(const Name: string): Integer;
    {Counts path separators in given name}
  var
    Idx: Integer; // loops thru name string
  begin
    Result := 0;
    for Idx := 1 to Length(Name) do
      if SysUtils.IsPathDelimiter(Name, Idx) then
        Inc(Result);
  end;

  function IsServerName(const Name: string): Boolean;
    {Returns true if Names is in form \\Server\Share}
  begin
    Result := (SysUtils.AnsiPos('\\', Name) = 1)
      and (CountPathDelims(Name) = 3);
  end;
  // ---------------------------------------------------------------------------
begin
  // Check if we have a drive, server/share or root path, and exit if so
  // (we can't apply file search to any of these, so we return them unchanged
  if (FilePath = '')
    or (FilePath = '\')
    or ((Length(FilePath) = 2) and (FilePath[2] = ':'))
    or ((Length(FilePath) = 3) and (FilePath[2] = ':') and (FilePath[3] = '\'))
    or IsServerName(FilePath) then
  begin
    Result := FilePath;
    Exit;
  end;
  // Do a file search on file: this is used to expand name
  Success := SysUtils.FindFirst(FilePath, SysUtils.faAnyFile, SR);
  try
    if Success = 0 then
      ExpandedName := SR.FindData.cFileName
    else
      ExpandedName := '';
  finally
    SysUtils.FindClose(SR);
  end;
  // Check if there's any part of path we've not handled, and convert it if so
  PrevPath := SysUtils.ExtractFileDir(FilePath);
  if PrevPath <> '' then
  begin
    // We have unprocessed part of path: expand that
    Result := ShortToLongFilePath(PrevPath);
    // Appended currently expanded name to path
    if (Result <> '') and (Result[Length(Result)] <> '\') then
      Result := Result + '\';
    Result := Result + ExpandedName;
  end
  else
    // No earlier parts of path: just record expanded name
    Result := ExpandedName;
end;
Kind of Snippet: Routine
Required units: SysUtils.
Required snippets: None.
Supported Compilers:
 D2   D3   D4   D5   D6   D7  D2005
(Win32)
D2006
(Win32)
D2007 D2009
(Win32)
D2010
(Win32)
Free
Pascal
Red LED Green LED Green LED Green LED Yellow LED Yellow LED Green LED Green LED Yellow LED Yellow LED Yellow LED Green LED