2008年7月28日

ファイル名を長い形式に変換

8.3形式の短いファイル名(Short File Name)の混じったフルパス名を全て長いファイル名(Long File Name)に変換するには、それぞれのディレクトリ/ファイル名を、FindFirstFileで得られるTWin32FindData(WIN32_FIND_DATA)構造体のcFileNameに格納されている長い名前で置き換えていきます。
uses
  Windows, SysUtils;

function ToLongFilename(const Filename: String): String;
var
  Path: String;
  BaseName: String;
  H: THandle;
  FD: TWin32FindData;
begin

  { Separate to path and base name }
  Path := ExtractFileDir(Filename);
  if Path = Filename then
  begin
    Result := Filename;
    Exit;
  end;
  BaseName := ExtractFileName(Filename);

  { Convert (recursive) }
  Path := ToLongFilename(Path);

  { Search using FindFirstFile/FindClose }
  Result := IncludeTrailingPathDelimiter(Path) + BaseName;
  H := Windows.FindFirstFile(PChar(Result),FD);
  if H = INVALID_HANDLE_VALUE then
  begin
    Exit;
  end;
  Windows.FindClose(H);

  Result := IncludeTrailingPathDelimiter(Path) + FD.cFileName;

end;

SFNに変換するGetShortPathNameのようにGetLongPathNameを使えばいいのかと思いきや、これではWindows NT 4.0が対象外になってしまいます。ターゲットをWindows 2000以降に限定するならいいのですけど。

0 件のコメント: