高手帮帮忙。删除指定目录下的所有文件和文件夹,还有拷贝指定的目录到指定的位置
功能有这几个需求:
1、删除指定目录下的所有文件和文件夹(不删除本目录)。如果有的系统正在使用没办法删除的就跳过。
2、删除指定目录连同本目录也删除
3、拷贝指定的目录(包括目录及下的文件)到指定的位置。
4、将指定的文件拷到指定的位置。如果有相同的文件名的文件存在则覆盖。
功能有这几个需求:
1、删除指定目录下的所有文件和文件夹(不删除本目录)。如果有的系统正在使用没办法删除的就跳过。
2、删除指定目录连同本目录也删除
3、拷贝指定的目录(包括目录及下的文件)到指定的位置。
4、将指定的文件拷到指定的位置。如果有相同的文件名的文件存在则覆盖。
sToDirName:String):Boolean;
var
hFindFile:Cardinal;
t,tfile:String;
sCurDir:String[255];
FindFileData:WIN32_FIND_DATA;
begin
//先保存当前目录
sCurDir:=GetCurrentDir;
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile< >INVALID_HANDLE_VALUE then
begin
if not DirectoryExists(sToDirName) then
ForceDirectories(sToDirName);
repeat
tfile:=FindFileData.cFileName;
if (tfile='.') or (tfile='..') then
Continue;
if FindFileData.dwFileAttributes=
FILE_ATTRIBUTE_DIRECTORY then
begin
t:=sToDirName+'\'+tfile;
if not DirectoryExists(t) then
ForceDirectories(t);
if sDirName[Length(sDirName)]< >'\' then
DoCopyDir(sDirName+'\'+tfile,t)
else
DoCopyDir(sDirName+tfile,sToDirName+tfile);
end
else
begin
t:=sToDirName+'\'+tFile;
CopyFile(PChar(tfile),PChar(t),True);
end;
until FindNextFile(hFindFile,FindFileData)=false;
FindClose(hFindFile);
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
//回到原来的目录下
ChDir(sCurDir);
result:=true;
end;---- 1.2拷贝目录的函数:CopyDir function CopyDir(sDirName:String;
sToDirName:string):Boolean;
begin
if Length(sDirName)< =0 then
exit;
//拷贝...
Result:=DoCopyDir(sDirName,sToDirName);
end;---- 2、删除目录 ---- 删除目录与拷贝目录很类似,但为了能删除位于根目录下的一个空目录,需要在辅助函数中设置一个标志变量,即:如果删除的是空目录,则置bEmptyDir为True,这一句已经用深色框表示了。 ---- 2.1删除目录的递归辅助函数:DoRemoveDir function DoRemoveDir(sDirName:String):Boolean;
var
hFindFile:Cardinal;
tfile:String;
sCurDir:String;
bEmptyDir:Boolean;
FindFileData:WIN32_FIND_DATA;
begin
//如果删除的是空目录,则置bEmptyDir为True
//初始时,bEmptyDir为True
bEmptyDir:=True;
//先保存当前目录
sCurDir:=GetCurrentDir;
SetLength(sCurDir,Length(sCurDir));
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile< >INVALID_HANDLE_VALUE then
begin
repeat
tfile:=FindFileData.cFileName;
if (tfile='.') or (tfile='..') then
begin
bEmptyDir:=bEmptyDir and True;
Continue;
end;
//不是空目录,置bEmptyDir为False
bEmptyDir:=False;
if FindFileData.dwFileAttributes=
FILE_ATTRIBUTE_DIRECTORY then
begin
if sDirName[Length(sDirName)]< >'\' then
DoRemoveDir(sDirName+'\'+tfile)
else
DoRemoveDir(sDirName+tfile);
if not RemoveDirectory(PChar(tfile)) then
result:=false
else
result:=true;
end
else
begin
if not DeleteFile(PChar(tfile)) then
result:=false
else
result:=true;
end;
until FindNextFile(hFindFile,FindFileData)=false;
FindClose(hFindFile);
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
//如果是空目录,则删除该空目录
if bEmptyDir then
begin
//返回上一级目录
ChDir('..');
//删除空目录
RemoveDirectory(PChar(sDirName));
end; //回到原来的目录下
ChDir(sCurDir);
result:=true;
end;---- 2.2删除目录的函数:DeleteDir function DeleteDir(sDirName:String):Boolean;
begin
if Length(sDirName)< =0 then
exit;
//删除...
Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName);
end;---- 3、移动目录 ---- 有了拷贝目录和删除目录的函数,移动目录就变得很简单,只需顺序调用前两个函数即可: function MoveDir(sDirName:String;
sToDirName:string):Boolean;
begin
if CopyDir(sDirName,sToDirName) then
if RemoveDir(sDirName) then
result:=True
else
result:=false;
end;///////////////////////////////////////////////
procedure TForm1.Button2Click(Sender: TObject);
var
OpStruc: TSHFileOpStruct;
frombuf, tobuf: Array [0..128] of Char;
Begin
FillChar( frombuf, Sizeof(frombuf), 0 );
FillChar( tobuf, Sizeof(tobuf), 0 );
StrPCopy( frombuf, 'd:\brief\*.*' );
StrPCopy( tobuf, 'd:\temp\brief' );
With OpStruc DO Begin
Wnd:= Handle;
wFunc:= FO_COPY;
pFrom:= @frombuf;
pTo:=@tobuf;
fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:= False;
hNameMappings:= Nil;
lpszProgressTitle:= Nil; end;
ShFileOperation( OpStruc );
end;无须递归的删除uses shellapi;{$R *.DFM} procedure TForm1.Button1Click(Sender: TObject);Var T:TSHFileOpStruct; P:String;begin P:='d:\address';//这里改成你要删除的任意目录名 With T do Begin Wnd:=0; wFunc:=FO_DELETE; pFrom:=Pchar(P); pTo:=nil; fFlags:=FOF_ALLOWUNDO+FOF_NOCONFIRMATION+FOF_NOERRORUI;//标志表明允许恢复,无须确认并不显示出错信息 hNameMappings:=nil; lpszProgressTitle:='正在删除文件夹'; fAnyOperationsAborted:=False; End; SHFileOperation(T);end;
var
OpStruc: TSHFileOpStruct;
FromBuf, ToBuf: Array [0..128] of Char;
begin
lab_state.Caption := '准备拷贝文件';
FillChar( FromBuf, Sizeof(FromBuf), 0 );
FillChar( ToBuf, Sizeof(ToBuf), 0 );
StrPCopy( FromBuf, Pchar(ExtractFilePath(ParamStr(0)))+'Bridge' );
StrPCopy( ToBuf, Pchar(lab_ml.Caption) );
// 设置O p S t r u c
with OpStruc do
begin
Wnd:= Handle;
wFunc:= FO_COPY;
pFrom:= @FromBuf;
pTo := @ToBuf ;
fFlags := FOF_NOCONFIRMATION;// or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:= False;
hNameMappings:= nil;
lpszProgressTitle:= nil;
end ;if SHFileOperation( OpStruc ) = 0 then
lab_state.Caption := '文件拷贝完成;end;
自己遍历就可靠很多,看需求的