お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"長いファイル名を取得(ネットワーク対応)"

この発言に対し以下のコメントが寄せられています
#01119 わいわい さん Re: 長いファイル名を取得(ネットワーク対

 みなさんこんにちは、稀杜です。  過去に何度もでていますが、短いファイル名から長いファイル名を得る関数 です。  ネットワークドライブに対応&関数内関数を使用してシンプルにしました。  おまけで短いファイル名を得る関数もつけました。 { function IncludeTrailingBackslash(const S: string): string; //パス名の末尾に'\'を付け加える(Delphi5以降では既存の関数) begin if IsPathDelimiter(S, Length(S)) then Result := S else Result := S + '\'; end; } function GetLongPathOf(const AFileName: string): string; //長いファイル名を求める(ネットワークドライブ対応) var LDrivePathLength: Integer; function GetLongNameOf(const AFilePath: string): string; var SearchRec: TSearchRec; begin //UNC対応 if Length(AFilePath) = LDrivePathLength then begin Result := UpperCase(AFilePath); end else begin if FindFirst(AFilePath, faAnyFile, SearchRec) = 0 then begin //AFilePathが見つかったとき Result := IncludeTrailingBackslash(GetLongNameOf( ExtractFileDir(AFilePath))) + SearchRec.Name; end else begin Result := ''; end; FindClose(SearchRec); end; end; begin Result := ''; if FileExists(AFileName) then begin LDrivePathLength := Length(ExtractFileDrive(AFileName)); if LDrivePathLength = 2 then LDrivePathLength := 3; //UNCでないとき('X:\' 3文字) Result := GetLongNameOf(AFileName); end; end; function GetShortPathOf(const AFileName: string): string; //短いファイル名を求める(Delphi4以降ではExtractShortPathName()がある) begin Result := ExtractShortPathName(AFileName); { //Delphi3以前では SetLength(Result, MAX_PATH); SetLength(Result, GetShortPathName(PChar(AFileName), PChar(Result), MAX_PATH)); } end; ---ここまで--- 使用例: procedure TForm1.Button1Click(Sender: TObject); var s: string; begin s := Application.ExeName; s := GetShortPathOf(s); Caption := s; Sleep(3000); s := GetLongPathOf(s); Caption := s; end; 注: ネットワークのコンピュータ名や共有名にスペースが含まれていた場合の 動作確認はしていません。 参考: nifty:FDELPHI/MES/15/27 nifty:FDELPHI/MES/15/147 nifty:FDELPHI/MES/16/564 nifty:FDELPHI/MES/16/879 など 2000/06/20(Tue) 03:04 YQW11350 kito@kun.ne.jp 稀杜 http://hp.vector.co.jp/authors/VA017148/ Original document by 稀杜 氏 ID:(YQW11350)



ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum