首页 > 其他分享 >delphi 路径操作函数

delphi 路径操作函数

时间:2023-11-16 19:35:40浏览次数:32  
标签:路径 函数 delphi ceshi Lines System Add TPath Memo1

路径操作函数

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

标签:路径,函数,delphi,ceshi,Lines,System,Add,TPath,Memo1
From: https://www.cnblogs.com/txgh/p/17837089.html

相关文章

  • c语言 函数参数个数影响
    参考:https://blog.csdn.net/Cheatscat/article/details/79306021https://blog.csdn.net/Dr_Haven/article/details/89383342一个函数的参数的数目过多(尤其是超过8个)显然是一种不可取的编程风格。参数的数目直接影响调用函数的速度,参数越多,调用函数越慢。参数的数目少,程序就显得......
  • 无涯教程-Dart - Optional Positional Parameter函数
    要指定可选的位置参数,请使用方括号[]。语法voidfunction_name(param1,[optional_param_1,optional_param_2]){}如果未传递可选参数的值,则将其设置为NULL。示例voidmain(){test_param(123);}test_param(n1,[s1]){print(n1);print(s1);}它......
  • 第六章 消息认证和哈希函数 —— 现代密码学(杨波)课后题答案解析
    第五章作业参考答案1.6.1.3节的数据认证算法是由CBC模式的DES定义的,其中初始向量取为0,试说明使用CFB模式也可获得相同的结果。解:设需认证的数据分为64比特长的分组,D1,D2,…,DN,其中DN不够64比特则右边补0,由题设,数据认证算法相当于在CBC模式中初始向量取为0,并按如下关系进行:   ......
  • 无涯教程-Dart - Parameterized Function函数
    参数是一种将值传递给函数的机制,参数是函数签名的一部分,参数值在调用过程中传递给函数,除非明确指定,否则传递给函数的值的数量必须与定义的参数的数量匹配。语法Function_name(data_typeparam_1,data_typeparam_2[…]){//statements}示例voidmain(){test_pa......
  • JS判断变量的具体数据类型封装函数
    封装函数为://返回传入值的数据类型functionGetValueType(val){vartype=typeofval//object需要使用Object.prototype.toString.call判断if(type==='object'){vartypeStr=Object.prototype.toString.call(val)//解析[objectStr......
  • 图- 最短路径
    图的最短路径最小生成树与最短路径最小生成树包含图中的所有点。能保证整个树中的所有路径之和最小。最短路径不一定包含图中的所有结点。(有向图,部分结点无法以最短路方式被加入)能保证从一点到图中其他点的路径最小。Dijkstra迪杰斯特拉算法Dijkstra按路径长度递增的......
  • VSCode------设置自动补全函数的括号
    一:VSCode设置自动补全函数的括号操作步骤1.1 寻找setting.json配置文件    Ctrl+Shift+P (Mac:command+Shift+P) 1.2编辑并保存配置内容 "typescript.suggest.completeFunctionCalls": true,  "javascript.suggest.completeFunctionCalls": ......
  • shell脚本定义变量和文件路径拼接
    在shell脚本定义变量为xx="xxx"例如把一个路径或文件名定义为一个变量inputPath="/mnt/RNASeq/Result"fileName="202308071824_210901003_2D230327074US2S2745DX"在路径"/mnt/RNASeq/Result"下面有多个文件夹,例如:L01、L02、···每个文件夹下存在多个fa文件,例如“2023080......
  • 你真的了解字符截取函数substr吗?php字符截取函数substr参数的6种情况。分别是:正正 负
    <?php$str='123456789abcd';echo'<br/>';echo'原字符:'.$str;echo'<br/>';//情况1正正++从指定位置开始截取3个echo'1正正substr($str,0,3):'.substr($str,0,3);//123echo'<br/>';//情况2......
  • Log4Delphi日志学习
    转载请注明出处:https://www.cnblogs.com/coder163/p/9309717.htmlhttps://log4delphi.sourceforge.net/tutorial.htmlLog4D下载:官网地址导入Delphi:Tool-->Options-->EnvironmentOptions--->DelphiOptions--Library-->Librarypath 三个目录使用载入配置文件菜单--->P......