delphi 怎样获取文件名不带后缀

delphi 怎样获取文件名不带后缀,第1张

小写了一个函数,取文件的。

function GetFileName(str:string):string

var

i:Integer

x,y:string

begin

x:=ExtractFileName(str) //取文件名+扩展名,不包含文件路径

y:=ExtractFileExt(str) //取文件的扩展名

i:=Pos(y,x) //确定扩展名所在位置

if i<>0 then

begin

Result:=Copy(x,0,i-1) //复制文件名,忽略掉后面的扩展名

end

else begin

Result:=''

end

end

使用的时候直接这样就行

GetFileName(TIdAttachment(Msg.MessageParts.Items[intIndex]).Filename)

输出:科比

用FindFirst,FindNext,FindClose

procedure SearchFileEx(const Dir, Ext: stringFiles: TStrings)

var

Found: TSearchRec

i: integer

Dirs: TStrings

Finished: integer

StopSearch: Boolean

begin

StopSearch := False

Dirs := TStringList.Create

Finished := FindFirst(Dir + '*.*', 63, Found)

while (Finished = 0) and not (StopSearch) do

begin

if (Found.Name <>'.') then

begin

if (Found.Attr and faDirectory) = faDirectory then

Dirs.Add(Dir + Found.Name)

else

if Pos(UpperCase(Ext), UpperCase(Found.Name)) >0 then

Files.Add(Dir + Found.Name)

end

Finished := FindNext(Found)

end

FindClose(Found)

if not StopSearch then

for i := 0 to Dirs.Count - 1 do

SearchFileEx(Dirs[i], Ext, Files)

Dirs.Free

end

3

procedure FindSubDir(DirName: stringFileString: TStrings)

var

searchRec: TsearchRec

begin

//找出所有下级子目录。

if (FindFirst(DirName + '*.*', faDirectory, SearchRec) = 0) then

begin

if IsValidDir(SearchRec) then

FileString.Add(DirName + SearchRec.Name)

while (FindNext(SearchRec) = 0) do

begin

if IsValidDir(SearchRec) then

FileString.Add(DirName + SearchRec.Name)

end

end

FindClose(SearchRec)

end

function IsValidDir(SearchRec: TSearchRec): Boolean

begin

if (SearchRec.Attr = 16) and (SearchRec.Name <>'.') and (SearchRec.Name <>'..') then

Result := True

else

Result := False

end


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11455646.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存