delphi 路径操作函数

发布时间 2023-11-16 19:28:48作者: txgh

路径操作函数

System.SysUtils.AnsiCompareFileName

根据当前语言环境比较文件名。

Windows 下不区分大小写,在 MAC OS 下区分大小写。

在不使用多字节字符集 (MBCS) 的 Windows 区域设置下,AnsiCompareFileNameAnsiCompareText 相同。在 MAC OSLinux 下,AnsiCompareFileNameAnsiCompareStr 相同。

begin
  if SameFileName('D:\ceshi\新建文件夹\ceshi.txt', 'D:\ceshi\新建文件夹\ceshi.txt') then
    Memo1.Lines.Add('文件名相同');
  //输出 文件名相同  
  if AnsiCompareFileName('ceshi.txt', 'CESHI.txt') = 0 then
    Memo1.Lines.Add('相等,Windows下不区分大小写')
  else
    Memo1.Lines.Add('不相等');
  //输出 相等,Windows下不区分大小写
end;

System.SysUtils.AnsiLowerCaseFileName

将文件名转换为小写。

begin
  Memo1.Lines.Add(AnsiLowerCaseFileName('CESHI.txt'));
  //输出 ceshi.txt
end;

System.IOUtils.TPath.ChangeExtension

class function ChangeExtension(const Path, Extension: string): string;

更改给定路径指示的文件或目录的扩展名。

begin
  Memo1.Lines.Add(TPath.ChangeExtension('C:\Users\Administrator\Desktop\ceshi.txt', 'xml'));
  //输出 C:\Users\Administrator\Desktop\ceshi.xml
end;

System.SysUtils.ChangeFileExt

function ChangeFileExt(const FileName, Extension: string): string;

更改文件名的扩展名。

begin
  Memo1.Lines.Add(ChangeFileExt('ceshi.txt', '.xml'));
  //输出 ceshi.xml
end;

System.SysUtils.ChangeFilePath

更改文件名的路径。

begin
  Memo1.Lines.Add(ChangeFilePath('C:\Users\Administrator\Desktop\ceshi.txt', 'D:\'));
  //输出 D:\ceshi.txt
end;

System.SysUtils.DirectoryExists

判断指定目录是否存在。

begin
  if DirectoryExists('D:\ceshi') then
    Memo1.Lines.Add('文件目录存在')
  else
    Memo1.Lines.Add('文件目录不存在');
end;

System.IOUtils.TPath.Combine

组合两个路径字符串。

begin
  Memo1.Lines.Add(TPath.Combine('D:\ceshi\新建文件夹', '新建文件夹 (2)'));
  //输出 D:\ceshi\新建文件夹\新建文件夹 (2)
end;

System.IOUtils.TPath.DriveExists

检查给定路径中使用的驱动器号是否实际存在。

var
  vPath: string;
begin
  vPath := 'D:\ceshi2';
  if TPath.DriveExists(vPath) then
  begin
    Memo1.Lines.Add('驱动器号存在');
    if not DirectoryExists(vPath) then
      Memo1.Lines.Add('文件目录不存在');
  end;
end;

System.SysUtils.ExcludeTrailingBackslash

返回不带尾部分隔符的路径名。

begin
  Memo1.Lines.Add(ExcludeTrailingBackslash('D:\ceshi\新建文件夹\'));
  //输出 D:\ceshi\新建文件夹
end;

System.SysUtils.ExcludeTrailingPathDelimiter

返回不带尾部分隔符的路径名。

ExcludeTrailingBackslash 相同。

System.SysUtils.ExpandFileName

返回相对文件名的完整路径名。

begin
  //相对于程序运行路径的完整路径
  Memo1.Lines.Add(ExpandFileName('新建文件夹\ceshi'));
  //输出 D:\Project1\Win32\Debug\新建文件夹\ceshi
end;

System.SysUtils.ExpandFileNameCase

返回区分大小写的文件系统上相对文件名的完整路径名。

Windows 下与 ExpandFileName 相同。在 MAC OSLinux 下查找文件

uses System.TypInfo;

var
  vMatch: TFilenameCaseMatch;
begin
  Memo1.Lines.Add(ExpandFileNameCase('ceshi.txt', vMatch));
  //输出 C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Win32\Debug\ceshi.txt
  Memo1.Lines.Add(GetEnumName(TypeInfo(TFilenameCaseMatch), Ord(vMatch)));
  //输出 mkNone
end;

System.SysUtils.ExpandUNCFileName

如果合适,以 UNC 格式返回文件名的完整路径。

begin
  //在“网络位置”中
  Memo1.Lines.Add(ExpandUNCFileName('ceshi.txt'));
  //输出 \\192.168.1.1\ceshi\ceshi.txt
end;

System.SysUtils.ExtractFileDir

从文件名中提取驱动器和目录部分。

begin
  Memo1.Lines.Add(ExtractFileDir('D:\ceshi\新建文件夹\ceshi.txt'));
  //输出 D:\ceshi\新建文件夹
end;

System.SysUtils.ExtractFileDrive

返回文件名的驱动器部分。

begin
  Memo1.Lines.Add(ExtractFileDrive('D:\ceshi\新建文件夹\ceshi.txt'));
  //输出 D:
end;

System.SysUtils.ExtractFileExt

返回文件名的扩展名部分。

begin
  Memo1.Lines.Add(ExtractFileExt('ceshi.txt'));
  //输出 .txt
  Memo1.Lines.Add(ExtractFileExt('D:\ceshi\新建文件夹\ceshi.txt'));
  //输出 .txt
end;

System.SysUtils.ExtractFileName

提取文件名的名称和扩展名部分。

begin
  Memo1.Lines.Add(ExtractFileName('ceshi.txt'));
  //输出 ceshi.txt
  Memo1.Lines.Add(ExtractFileName('D:\ceshi\新建文件夹\ceshi.txt'));
  //输出 ceshi.txt
end;

System.SysUtils.ExtractFilePath

返回文件名的驱动器和目录部分。

begin
  Memo1.Lines.Add(ExtractFilePath('ceshi.txt'));
  //输出 
  Memo1.Lines.Add(ExtractFilePath('D:\ceshi\新建文件夹\ceshi.txt'));
  //输出 D:\ceshi\新建文件夹\
end;

System.SysUtils.ExtractRelativePath

返回相对于特定基目录的相对路径名。

begin
  //路径需要带“\”,否则返回错误
  ExtractRelativePath('D:\ceshi\新建文件夹\', 'D:\ceshi\新建文件夹 (2)\');
  //输出 ..\新建文件夹 (2)\
end;

System.SysUtils.ExtractShortPathName

将文件名转换为简短的8.3格式。

begin
  //文件必须存在才返回
  Memo1.Lines.Add(ExtractShortPathName('C:\Program Files (x86)\Embarcadero\Studio\22.0\bin\bds.exe'));
  //输出 C:\PROGRA~2\EMBARC~1\Studio\22.0\bin\bds.exe
end;

System.IOUtils.TPath.GetAttributes

class function GetAttributes(const Path: string; FollowLink: Boolean = True): TFileAttributes;

返回文件或目录属性。

uses System.IOUtils, System.TypInfo;

var
  vAttributes: TFileAttributes;
  vAttrib: TFileAttribute;
begin
  vAttributes := TPath.GetAttributes('D:\ceshi\新建文件夹\');
  for vAttrib in vAttributes do
    Memo1.Lines.Add(GetEnumName(TypeInfo(TFileAttribute), Ord(vAttrib)));
  //输出 faDirectory
  vAttributes := TPath.GetAttributes('D:\ceshi\新建文件夹\ceshi.txt');
  for vAttrib in vAttributes do
    Memo1.Lines.Add(GetEnumName(TypeInfo(TFileAttribute), Ord(vAttrib)));
  //输出 GetAttributes
end;

System.IOUtils.TPath.GetDirectoryName

提取文件名的驱动器和目录部分。

begin
  Memo1.Lines.Add( TPath.GetDirectoryName('D:\ceshi\新建文件夹\ceshi.txt'));
  //输出 D:\ceshi\新建文件夹
end;

System.IOUtils.TPath.GetExtendedPrefix

返回给定路径的扩展前缀类型。

begin
  TPath.GetExtendedPrefix('D:\ceshi\新建文件夹\');
  //输出 pptNoPrefix
end;

System.IOUtils.TPath.GetExtension

提取文件名的扩展名部分。

begin
  Memo1.Lines.Add(TPath.GetExtension('ceshi.txt'));
  //输出 .txt
  Memo1.Lines.Add(TPath.GetExtension('D:\ceshi\新建文件夹\ceshi.txt'));
  //输出 .txt
end;

System.IOUtils.TPath.GetFileName

提取文件名的名称和扩展名部分。

begin
  Memo1.Lines.Add(TPath.GetFileName('ceshi.txt'));
  //输出 ceshi.txt
  Memo1.Lines.Add(TPath.GetFileName('D:\ceshi\新建文件夹\ceshi.txt'));
  //输出 ceshi.txt
end;

System.IOUtils.TPath.GetFileNameWithoutExtension

提取文件名的名称部分,不带扩展名。

begin
  Memo1.Lines.Add(TPath.GetFileNameWithoutExtension('ceshi.txt'));
  //输出 ceshi
  Memo1.Lines.Add(TPath.GetFileNameWithoutExtension('D:\ceshi\新建文件夹\ceshi.txt'));
  //输出 ceshi
end;

System.IOUtils.TPath.GetFullPath

返回给定路径的绝对路径。

begin
  Memo1.Lines.Add(TPath.GetFullPath('ceshi.txt'));
  //输出 C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Win32\Debug\ceshi.txt
end;

System.IOUtils.TPath.GetGUIDFileName

生成可用作唯一文件名的新 GUID。

begin
  Memo1.Lines.Add(TPath.GetGUIDFileName);
  //输出 17DC1DDB8C334D61A5499597CEC22D5E
end;

System.IOUtils.TPath.GetHomePath

返回用户的主路径。

begin
  Memo1.Lines.Add(TPath.GetHomePath);
  //输出 C:\Users\Administrator\AppData\Roaming
end;

System.IOUtils.TPath.GetRandomFileName

生成新的随机文件名。

begin
  Memo1.Lines.Add(TPath.GetRandomFileName);
  //输出 0EpRoGU5.7O1
end;

System.IOUtils.TPath.GetTempFileName

生成一个唯一的临时文件。

var
  vFile: string;
begin
  vFile := TPath.GetTempFileName;
  Memo1.Lines.Add(vFile);
  //输出 C:\Users\Administrator\AppData\Local\Temp\tmp8F56.tmp
  //不使用时删除
  DeleteFile(vFile);
end;

System.IOUtils.TPath.GetTempPath

返回系统临时目录的路径。

begin
  Memo1.Lines.Add(TPath.GetTempPath);
  //输出 C:\Users\Administrator\AppData\Local\Temp\
end;

System.IOUtils.TPath.HasExtension

检查给定文件名是否有扩展名部分。

begin
  if TPath.HasExtension('ceshi.txt') then
    Memo1.Lines.Add('包含扩展名');
  //输出 包含扩展名  
  if not TPath.HasExtension('D:\ceshi\新建文件夹\') then
    Memo1.Lines.Add('不包含扩展名');
  //输出 不包含扩展名
end;

System.IOUtils.TPath.HasValidFileNameChars

class function HasValidFileNameChars(const FileName: string; const UseWildcards: Boolean): Boolean;

检查给定文件名是否仅包含允许的字符。

UseWildcards 指定通配符是否被视为有效的文件名字符(例如星号或问号)。

begin
  if not TPath.HasValidFileNameChars('<ceshi>.txt', False) then
    Memo1.Lines.Add('包含特殊字符');
  //输出 包含特殊字符  
  if TPath.HasValidFileNameChars('ceshi*.txt', True) then
    Memo1.Lines.Add('包含通配符字符');
  //输出 包含通配符字符
end;

System.IOUtils.TPath.HasValidPathChars

class function HasValidPathChars(const Path: string; const UseWildcards: Boolean): Boolean;

检查给定路径字符串是否仅包含允许的字符。

UseWildcards 指定通配符是否被视为有效的文件名字符(例如星号或问号)。

begin
  if not TPath.HasValidPathChars('D:\ceshi\<新建文件夹>', False) then
    Memo1.Lines.Add('包含特殊字符');
  //输出 包含特殊字符  
  if TPath.HasValidPathChars('D:\ceshi\新建文件夹*', True) then
    Memo1.Lines.Add('包含通配符字符');
  //输出 包含通配符字符
end;

System.SysUtils.IncludeTrailingBackslash

返回带尾部分隔符的路径名。(在 Windows 上为 \,否则为 /)。

注意:包含此函数只是为了向后兼容。应改用 System.SysUtils.IncludeTrailingPathDelimiter

System.SysUtils.IncludeTrailingPathDelimiter

function IncludeTrailingPathDelimiter(const S: string): string;

返回带尾部分隔符的路径名。(在 Windows 上为 \,否则为 /)。

如果已经以尾部分隔符结尾,则原样返回;否则,附加分隔符。

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Add(IncludeTrailingPathDelimiter('D:\ceshi\新建文件夹'));
  //输出 D:\ceshi\新建文件夹\
end;

System.IOUtils.TPath.IsDriveRooted

class function IsDriveRooted(const Path: string): Boolean;

检查给定路径是否是绝对路径,并以驱动器号开头。

在 POSIX 上,始终返回 false,因为没有驱动器根。

begin
  if TPath.IsDriveRooted('D:\ceshi\新建文件夹\') then
    Memo1.Lines.Add('绝对路径');
  //输出 绝对路径
  if TPath.IsDriveRooted('D:') then
    Memo1.Lines.Add('绝对路径');
  //输出 绝对路径
end;

System.IOUtils.TPath.IsExtendedPrefixed

class function IsExtendedPrefixed(const Path: string): Boolean;

检查给定路径是否包含扩展前缀。

\\?\\\?\UNC\ 为前缀的路径是 Windows 特有的,长度可以非常大,并且不限于 255 个字符 (MAX_PATH)。在路径前面加上 \\?\ 可以解决长度超过 255 个字符问题。\\?\ 告诉 Windows API 禁用所有字符串解析并将其后面的字符串发送到文件系统。可以超出 Windows API 强制执行的 MAX_PATH 限制。

在 POSIX 上,始终返回 false,因为没有扩展前缀。

begin
  if TPath.IsExtendedPrefixed('\\?\D:\ceshi\新建文件夹') then
    Memo1.Lines.Add('包含扩展前缀');
  //输出 包含扩展前缀
end;

System.SysUtils.IsPathDelimiter

function IsPathDelimiter(const S: string; Index: Integer): Boolean;

指示字符串中指定位置的字符是否为路径分隔符。(在 Windows 上为 \,否则为 /)。位置索引从 1 开始。

begin
  if IsPathDelimiter('D:\ceshi\新建文件夹', 3) then
    Memo1.Lines.Add('第3个字符为路径分隔符\');
  //输出 第3个字符为路径分隔符\
end;

System.IOUtils.TPath.IsPathRooted

检查给定路径是否是绝对路径。

begin
  if TPath.IsPathRooted('D:\ceshi\新建文件夹\') then
    Memo1.Lines.Add('绝对路径');
  //输出 绝对路径
  if TPath.IsPathRooted('\ceshi\新建文件夹') then
    Memo1.Lines.Add('没有驱动器号开头的绝对路径');
  //输出 没有驱动器号开头的绝对路径
  if not TPath.IsPathRooted('ceshi\新建文件夹') then
    Memo1.Lines.Add('相对路径');
  //输出 绝对路径
end;

System.IOUtils.TPath.IsUNCPath

检查给定路径是否为 UNC 格式。UNC 格式路径以两个反斜杠字符为前缀(例如\\computer\folder)。

begin
  if TPath.IsUNCPath('\\192.168.1.1\ceshi') then
    Memo1.Lines.Add('UNC格式');
  //输出 UNC格式
  if not TPath.IsUNCPath('\\192.168.1.1\<ceshi>') then
    Memo1.Lines.Add('不正确的UNC格式');
  //输出 不正确的UNC格式
end;

System.IOUtils.TPath.IsUNCRooted

检查给定路径是否是 UNC 根路径。

在 POSIX 上,始终返回 false,因为没有扩展前缀。

procedure TForm1.Button1Click(Sender: TObject);
begin
  if TPath.IsUNCRooted('\\192.168.1.1\ceshi') then
    Memo1.Lines.Add('UNC根路径');
  //输出 UNC根路径
  if TPath.IsUNCRooted('\\192.168.1.1\<ceshi>') then
    Memo1.Lines.Add('UNC根路径');
  //输出 UNC根路径
end;

System.IOUtils.TPath.IsValidFileNameChar

检查文件名字符串中是否允许使用给定字符。

Windows 中特殊字符

#0, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16,  #17, #18, #19, #20, #21, #22, #23, #24, #25, #26, #27, #28, #29, #30, #31, ",  *, /, :, <, >, ?, \, | 

MacOS, iOS, Android, Linux 中特殊字符

#0, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16,  #17, #18, #19, #20, #21, #22, #23, #24, #25, #26, #27, #28, #29, #30, #31, / , ~. 
begin
  if not TPath.IsValidFileNameChar('*') then
    Memo1.Lines.Add('文件名中不能使用');
  //输出 文件名中不能使用
end;

System.IOUtils.TPath.IsValidPathChar

检查路径字符串中是否允许使用给定字符。

begin
  if not TPath.IsValidPathChar('|') then
    Memo1.Lines.Add('路径中不能使用');
  //输出 路径中不能使用
end;

Vcl.FileCtrl.MinimizeName

function MinimizeName(const Filename: TFileName; Canvas: TCanvas; MaxLen: Integer): TFileName;

获取可以在有限大小的在画布上绘制的文件名和路径的缩写名称。 缩短 Filename,使其可以在 MaxLen 长度限制内绘制。用点替换文件名路径部分中的目录,直到生成的名称符合指定的像素长度。Canvas 是要呈现缩写名称的画布,用于确定字体规格。

rebegin
  Memo1.Lines.Add(MinimizeName('D:\ceshi\新建文件夹\ceshi.txt', Self.Canvas, 150));
  //输出 D:\...\新建文件夹\ceshi.txt
end;

System.SysUtils.SameFileName

根据当前区域设置比较文件名。

AnsiCompareFileName 相同。

System.IOUtils.TPath.SetAttributes

class procedure SetAttributes(const Path: string; const Attributes: TFileAttributes);

设置文件或目录属性。

procedure TForm1.Button1Click(Sender: TObject);
begin
  //设置文件为只读
  TPath.SetAttributes('D:\ceshi\新建文件夹\ceshi.txt', [TFileAttribute.faReadOnly, TFileAttribute.faHidden]);
  //设置目录为隐藏
  TPath.SetAttributes('D:\ceshi\新建文件夹', [TFileAttribute.faHidden]);
end;
class function CreateSymLink(const Link, Target: string): Boolean;

创建符号链接。

注意:调用CreateSymLink时目标文件或目录必须存在。

Windows Vista 及更高版本的 Windows 上使用。

procedure TForm1.Button1Click(Sender: TObject);
begin
  //相当于CMD命令 mklink "D:\SymLink" "D:\ceshi\新建文件夹"
  TFile.CreateSymLink('D:\SymLink', 'D:\ceshi\新建文件夹');
end;

参考

创建符号链接

文件名中使用的字符集

Path Manipulation Routines