uses Windows,SysUtils,Classes,FileCtrl,ShellAPI; //判断文件或目录是否存在 function JudgeFDExists(const Name:String):Boolean; //删除文件或目录 function DelFD(const Name:String;CanUndo:Boolean=True):Boolean; //重命名文件或目录 function ReNameFD(OldName,NewName:String):Boolean; //移动文件或目录 function MoveFD(SouName,DesName:String):Boolean; //复制文件或目录 function CopyFD(SouName,DesName:String):Boolean; //判断文件或目录是否存在 function JudgeFDExists(const Name:String):Boolean; begin Result:=False; if (FileExists(Name)) or (DirectoryExists(Name)) then Result:=True; end; //删除文件或目录 { CanUndo =True,表示把文件或目录删除到回收站(默认方式) =False,表示真正删除 } function DelFD(const Name:String;CanUndo:Boolean=True):Boolean; var Fo:TSHFileOpStruct; begin if not JudgeFDExists(Name) then Raise Exception.Create('Cannot find the file or directory.') else begin FillChar(Fo,SizeOf(Fo),0); with Fo do begin Wnd:=0; wFunc:=FO_DELETE; pFrom:=PChar(Name+#0); pTo:=#0#0; if CanUndo then fFlags:=FOF_AllOWUNDO+FOF_NOCONFIRMATION else fFlags:=FOF_NOCONFIRMATION+FOF_SILENT; end; Result:=(SHFileOperation(Fo)=0); end; end; //重命名文件或目录 function ReNameFD(OldName,NewName:String):Boolean; var Fo:TSHFileOpStruct; begin if not JudgeFDExists(OldName) then Raise Exception.Create('Cannot find the file or directory.') else begin FillChar(Fo,SizeOf(Fo),0); with Fo do begin Wnd:=0; wFunc:=FO_RENAME; pFrom:=PChar(OldName+#0); pTo:=PChar(NewName+#0); fFlags:=FOF_NOCONFIRMATION+FOF_SILENT; end; Result:=(SHFileOperation(Fo)=0); end; end; //移动文件或目录 function MoveFD(SouName,DesName:String):Boolean; var Fo:TSHFileOpStruct; begin if not JudgeFDExists(SouName) then Raise Exception.Create('Cannot find the file or directory.') else begin FillChar(Fo,SizeOf(Fo),0); with Fo do begin Wnd:=0; wFunc:=FO_MOVE; pFrom:=PChar(SouName+#0); pTo:=PChar(DesName+#0); fFlags:=FOF_NOCONFIRMATION+FOF_SILENT; end; Result:=(SHFileOperation(Fo)=0); end; end; //复制文件或目录 function CopyFD(SouName,DesName:String):Boolean; var Fo:TSHFileOpStruct; begin if not JudgeFDExists(SouName) then Raise Exception.Create('Cannot find the file or directory.') else begin FillChar(Fo,SizeOf(Fo),0); with Fo do begin Wnd:=0; wFunc:=FO_COPY; pFrom:=PChar(SouName+#0); pTo:=PChar(DesName+#0); fFlags:=FOF_NOCONFIRMATION+FOF_SILENT; end; Result:=(SHFileOperation(Fo)=0); end; end;