D7遍历文件改名的问题

NC217 2017-11-05 06:39:24
function MakeFileList(Path,FileExt:string):TStringList ;
var
E1:integer;
sch:TSearchrec;
begin
E1:=0;
Result:=TStringlist.Create;
if rightStr(trim(Path), 1) <> '\' then
Path := trim(Path) + '\'
else
Path := trim(Path);
if not DirectoryExists(Path) then
begin
Result.Clear;
exit;
end;
if FindFirst(Path + '*', faAnyfile, sch) = 0 then
begin
repeat
Application.ProcessMessages;
if ((sch.Name = '.') or (sch.Name = '..')) then Continue;
if DirectoryExists(Path+sch.Name) then // 这个地方加上一个判断,可以区别子文件夹河当前文件夹的操作
begin
Result.AddStrings(MakeFileList(Path+sch.Name,FileExt));
end
else
begin
if (UpperCase(extractfileext(Path+sch.Name)) = UpperCase(FileExt)) or (FileExt='.*') then
Result.Add(Path+sch.Name);
end;
showmessage(sch.name); //显示每个文件夹里的文件名
renamefile(sch.name,concat(IntToStr(E1),'.elky') ); //改名字和后缀名
showmessage(sch.name);//不能正常显示修改后的名字
INC(E1);
until FindNext(sch) <> 0;
SysUtils.FindClose(sch);
end;
end;

不能正常修改文件夹里的文件名,请教大家我的代码要怎么写?
...全文
495 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
NC217 2017-11-06
  • 打赏
  • 举报
回复
子文件夹里的文件名我也要修改,这只是改当前文件夹里的所有文件的名字呀。
NewComer2017 2017-11-06
  • 打赏
  • 举报
回复
function searchfile(path:string):TStringList; var SearchRec:TSearchRec; found:integer; list:TStringList; begin list:=TStringList.Create; found:=FindFirst(path+'\*.*',faAnyFile,SearchRec); while found=0 do begin if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') and (SearchRec.Attr<>faDirectory) and (SearchRec.Name<>'Thumbs.db') then List.Add(SearchRec.Name); found:=FindNext(SearchRec); end; FindClose(SearchRec); form1.Memo1.Lines.AddStrings(list); end;
lyhoo163 2017-11-05
  • 打赏
  • 举报
回复
  Try
    renamefile(sch.name,concat(IntToStr(E1),'.elky') ); 
    sleep(200);                      // 值要测试  
    INC(E1);
  except
    on e:Exception do
    begin
      ShowMessage(e.Message);
    end;
  end;
这样,试试。
lyhoo163 2017-11-05
  • 打赏
  • 举报
回复
对于磁盘文件的操作,往往有时间差,命令发出后,磁盘的操作经过一段的时间后,才能成功。因此,你的操作就出错了。 两点建议: 1、加入Try组合语句,保证更名出错后,代码能自动退出此操作,并显示出错代码。 2、在磁盘文件操作命令后,加入等待时间差,比如:sleep语句。

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧