function FilesMaskOperation(const fromDir, toDir, fileMask: string; FileOp: Integer; EmptyDirRemove: Boolean; toRecycle: Boolean): Boolean; var fos: TSHFileOpStruct; _fromDir, _toDir: string; FSR, DSR: TFindRec; FindResult: Boolean; APath: string; begin APath := AddBackslash(fromDir); FindResult := FindFirst(APath + fileMask, FSR); try while FindResult do begin if FSR.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then begin Case FileOp of FO_COPY: begin fos.wFunc := FO_COPY; end; FO_MOVE: begin fos.wFunc := FO_MOVE; end; FO_DELETE: begin fos.wFunc := FO_DELETE; if toRecycle then fos.fFlags := fos.fFlags or FOF_ALLOWUNDO; end; FO_RENAME: begin fos.wFunc := FO_RENAME; end; else ; end; fos.fFlags := fos.fFlags or FOF_FILESONLY or FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR; _fromDir:= APath + FSR.Name + #0#0; _toDir:= AddBackslash(toDir) + FSR.Name + #0#0; ForceDirectories(ExtractFilePath(_toDir)); fos.pFrom := PChar(_fromDir); fos.pTo := PChar(_toDir); Result := (0 = ShFileOperation(fos)); end; FindResult := FindNext(FSR); end; FindResult := FindFirst(APath + '*.*', DSR); while FindResult do begin if ((DSR.Attributes and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) and not ((DSR.Name = '.') or (DSR.Name = '..')) then {Recursion} FilesMaskOperation(APath + DSR.Name, AddBackslash(toDir) + DSR.Name, fileMask, FileOp, EmptyDirRemove, toRecycle); FindResult := FindNext(DSR); end; finally FindClose(FSR); FindClose(DSR); if EmptyDirRemove then RemoveDir(APath); end; end; function CopyFiles(const fromDir, toDir, fileMask: string): Boolean; begin Result := FilesMaskOperation(fromDir, toDir, fileMask, FO_COPY, False, False); end; function MoveFiles(const fromDir, toDir, fileMask: string): Boolean; begin Result := FilesMaskOperation(fromDir, toDir, fileMask, FO_MOVE, True, False); end; function DelFiles(const fromDir, fileMask: string; toRecycle: Boolean ): Boolean; begin Result := FilesMaskOperation(fromDir, '', fileMask, FO_DELETE, True, toRecycle); end; |