看看我的代码,有参考价值不
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3167849
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
Button1:TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure ReplaceAllExt(AParentPath: string; AOrignExt, ADestExt: String);
[blue]// 路径, 原始扩展名,目标扩展名[/blue]
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.ReplaceAllExt(AParentPath: string; AOrignExt, ADestExt: String);
var
SR: TSearchRec;
nCount: Integer;
FileName: string;
begin
if not DirectoryExists(AParentPath) then begin
Application.MessageBox('文件夹不存在','提示', MB_OK +MB_ICONERROR);
Abort;
end;
nCount := FindFirst(AParentPath + '/*' + AOrignExt, faAnyFile, SR);// 这里设置你要搜索的文件
while nCount = 0 do begin
if (SR.Name <> '.') and (SR.Name <> '..') then begin
// 如果不是。和。。才继续
if SR.Attr = faDirectory then
// 如果是文件夹则递归
ReplaceAllExt(AParentPath + '/' + SR.Name, AOrignExt, ADestExt)
else begin
FileName := AParentPath + '/' + SR.Name;
// 修改文件名
ReNameFile(FileName, ChangeFileExt(FileName, ADestExt));
end;
end;
nCount := FindNext(SR);
end;
FindClose(SR);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
[blue]// 路径, 原始扩展名,目标扩展名[/blue]
ReplaceAllExt('c:/aa', '.gif', '.txt');
end;
end.