[Delphi (Object Pascal)] Delphi 把短文件名(dos 8.3)转换成长文件名 →→→→→进入此内容的聊天室

来自 , 2019-06-30, 写在 Delphi (Object Pascal), 查看 132 次.
URL http://www.code666.cn/view/0e2db0cb
  1. function ShortToLongFilePath(const FilePath: string): string;
  2. var
  3.   PrevPath: string;         // path before last file/dir in FilePath
  4.   ExpandedName: string;     // long form of file name
  5.   SR: SysUtils.TSearchRec;  // record used by file find functions
  6.   Success: Integer;         // indicates success in finding a file
  7.   // ---------------------------------------------------------------------------
  8.   function CountPathDelims(const Name: string): Integer;
  9.     {Counts path separators in given name}
  10.   var
  11.     Idx: Integer; // loops thru name string
  12.   begin
  13.     Result := 0;
  14.     for Idx := 1 to Length(Name) do
  15.       if SysUtils.IsPathDelimiter(Name, Idx) then
  16.         Inc(Result);
  17.   end;
  18.  
  19.   function IsServerName(const Name: string): Boolean;
  20.     {Returns true if Names is in form \\Server\Share}
  21.   begin
  22.     Result := (SysUtils.AnsiPos('\\', Name) = 1)
  23.       and (CountPathDelims(Name) = 3);
  24.   end;
  25.   // ---------------------------------------------------------------------------
  26. begin
  27.   // Check if we have a drive, server/share or root path, and exit if so
  28.   // (we can't apply file search to any of these, so we return them unchanged
  29.   if (FilePath = '')
  30.     or (FilePath = '\')
  31.     or ((Length(FilePath) = 2) and (FilePath[2] = ':'))
  32.     or ((Length(FilePath) = 3) and (FilePath[2] = ':') and (FilePath[3] = '\'))
  33.     or IsServerName(FilePath) then
  34.   begin
  35.     Result := FilePath;
  36.     Exit;
  37.   end;
  38.   // Do a file search on file: this is used to expand name
  39.   Success := SysUtils.FindFirst(FilePath, SysUtils.faAnyFile, SR);
  40.   try
  41.     if Success = 0 then
  42.       ExpandedName := SR.FindData.cFileName
  43.     else
  44.       ExpandedName := '';
  45.   finally
  46.     SysUtils.FindClose(SR);
  47.   end;
  48.   // Check if there's any part of path we've not handled, and convert it if so
  49.   PrevPath := SysUtils.ExtractFileDir(FilePath);
  50.   if PrevPath <> '' then
  51.   begin
  52.     // We have unprocessed part of path: expand that
  53.     Result := ShortToLongFilePath(PrevPath);
  54.     // Appended currently expanded name to path
  55.     if (Result <> '') and (Result[Length(Result)] <> '\') then
  56.       Result := Result + '\';
  57.     Result := Result + ExpandedName;
  58.   end
  59.   else
  60.     // No earlier parts of path: just record expanded name
  61.     Result := ExpandedName;
  62. end;
  63. //delphi/2223

回复 "Delphi 把短文件名(dos 8.3)转换成长文件名"

这儿你可以回复上面这条便签

captcha