急,RenameFile函数在myRenameFile递归时中未起作用
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, FileCtrl;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Label1: TLabel;
DirectoryListBox1: TDirectoryListBox;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
function IsValidDir(SearchRec: TSearchRec):Boolean;
function myRenamefile(MainPath: string):boolean;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var NowPath: string;
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
NowPath := DirectoryListBox1.Directory;
myRenamefile(NowPath+'\*.*');
BitBtn1.Tag := BitBtn1.Tag+1;
Application.MessageBox('转换完毕','提示',MB_OK+64);
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
close;
Application.Terminate;
end;
function TForm1.myRenamefile(MainPath: string):boolean;
var
i: Integer;
tmp: string;
subdir: TStrings;
SearchRec: TSearchRec;
found: boolean;
begin
found := false;
i := 1;
if length(NowPath)<>0 then
i := FindFirst(MainPath,faAnyFile, SearchRec)
else
Application.MessageBox('请先从目录列表中选择转换路径!','提示',MB_OK+64);
subdir := TStringList.Create;
//找出所有下级子目录
if i=0 then
begin
if IsValidDir(SearchRec) then
subdir.Add(SearchRec.Name);
while (FindNext(SearchRec) = 0) do
begin
if IsValidDir(SearchRec) then
subdir.Add(SearchRec.Name);
end;
i := FindFirst(MainPath,faAnyFile, SearchRec);
end;
while i=0 do
begin
//文件改名
if (BitBtn1.Tag mod 2)=0 then
tmp := Lowercase(SearchRec.Name)
else
tmp := Uppercase(SearchRec.Name);
RenameFile(SearchRec.Name,tmp);
i := FindNext(SearchRec);
end;
//递归部分
for i := 0 to subdir.Count-1 do
found := myRenamefile(NowPath+'\'+subdir.Strings[i]+'\*.*');
result := found;
//释放资源
subdir.free;
FindClose(SearchRec);
end;
//查询是否有子目录
function TForm1.IsValidDir(SearchRec: TSearchRec):Boolean;
begin
if (SearchRec.Attr=16) and (SearchRec.name<>'.') and (SearchRec.name<>'..') then
Result := true
else
Result := false;
end;
end.
跟踪文件改名操作时发现可以正确更改根目录下文件,对子目录下文件却无法正常操作,动态跟踪了tmp以及SearchRec.name的值,没有任何问题,所以怀疑是不是RenameFile罢工了@_@,请高手点拔