读书人

请教怎么将一个指定目录下所有文件的后

发布时间: 2012-02-17 17:50:41 作者: rapoo

请问如何将一个指定目录下所有文件的后缀由*.BMP改为*.PBM?
如题

[解决办法]
procedure TForm1.Button2Click(Sender: TObject);
procedure GetFile(PathName: string);
var
FindData: TWin32FindData;
hf:THandle;
b:boolean;
tmpstr:string;
tempFolder:string;
str:string;
begin
hf := Windows.FindFirstFile(PChar(PathName + '\*.BMP '), FindData);
if hf = INVALID_HANDLE_VALUE then exit;
b := true;
while b do
begin
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
begin
str:=PathName+ '\ '+string(FindData.cFileName);
MoveFile(PChar(str),PChar(StringReplace(str, '.BMP ', '.PBM ',[rfReplaceAll])));
end
else
begin
tmpstr := FindData.cFileName + ' ';
if (tmpstr <> '. ') and (tmpstr <> '.. ') then
begin
tempFolder:=tempFolder+string(FindData.cFileName)+ '\ ';
GetFile(PathName + '\ ' + FindData.cFileName);
end;
end;
b := windows.FindNextFile(hf,FindData);
end;
end;

begin
GetFile( 'd:\123 ' );
end;

[解决办法]
procedure BatchRenameFile(sPath, sOldExt, sNewExt: String);
var
SR : TSearchRec;
begin
if sPath[Length(sPath)] <> '\ ' then sPath := sPath + '\ ';
if FindFirst(sPath + '* ' + sOldExt, faAnyFile, SR) = 0 then
begin
repeat
RenameFile(sPath + SR.Name, ChangeFileExt(sPath + SR.Name, sNewExt));
until FindNext(SR) <> 0;
FindClose(SR);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
BatchRenameFile( 'D:\ ', '.BMP ', '.PBM ');
end;

读书人网 >.NET

热点推荐