怎样复制、删除一个文件夹(该文件夹下包括文件或文件夹)?(50分)

  • 主题发起人 主题发起人 45060203
  • 开始时间 开始时间
4

45060203

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样复制、删除一个文件夹(该文件夹下包括文件或文件夹)?
 
FUNCTION DCF(s,t:String):Boolean;<br>VAR<br>&nbsp; &nbsp; &nbsp;ShFileopStruct: TShFileOpStruct;<br>BEGIN<br>&nbsp; WITH ShFileOpStruct DO BEGIN<br>&nbsp; &nbsp; &nbsp; Wnd:=0;<br>&nbsp; &nbsp; &nbsp; wFunc:=Fo_Copy;//fo_delete<br>&nbsp; &nbsp; &nbsp; pFrom:=PChar(s);<br>&nbsp; &nbsp; &nbsp; pTO:=PChar(t);<br>&nbsp; &nbsp; &nbsp; fFlags:= FOF_NOCONFIRMATION OR FOF_ALLOWUNDO ;<br><br>&nbsp; END;<br>&nbsp; &nbsp;DCF := ShFileOperation(ShFileOpStruct)=0;<br>END;<br>
 
这个函数不能在nt上用
 
function GetDirectory: String;<br>begin<br>&nbsp; if not SelectDirectory(Result, [sdAllowCreate, sdPerformCreate, sdPrompt], 0) then<br>&nbsp; &nbsp; Result := EmptyStr;<br>end;<br><br>procedure CopyDirectoryTree(AHandle: THandle; const AFromDirectory, AToDirectory: String);<br>var<br>&nbsp; SHFileOpStruct: TSHFileOpStruct;<br>&nbsp; FromDir: PChar;<br>&nbsp; ToDir: PChar;<br>begin<br><br>&nbsp; GetMem(FromDir, Length(AFromDirectory)+2);<br>&nbsp; try<br>&nbsp; &nbsp; GetMem(ToDir, Length(AToDirectory)+2);<br>&nbsp; &nbsp; try<br><br>&nbsp; &nbsp; &nbsp; FillChar(FromDir^, Length(AFromDirectory)+2, 0);<br>&nbsp; &nbsp; &nbsp; FillChar(ToDir^, Length(AToDirectory)+2, 0);<br><br>&nbsp; &nbsp; &nbsp; StrCopy(FromDir, PChar(AFromDirectory));<br>&nbsp; &nbsp; &nbsp; StrCopy(ToDir, PChar(AToDirectory));<br><br>&nbsp; &nbsp; &nbsp; with SHFileOpStruct do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Wnd &nbsp; &nbsp;:= AHandle; &nbsp; // Assign the window handle<br>&nbsp; &nbsp; &nbsp; &nbsp; wFunc &nbsp;:= FO_COPY; &nbsp;// Specify a file copy<br>&nbsp; &nbsp; &nbsp; &nbsp; pFrom &nbsp;:= FromDir;<br>&nbsp; &nbsp; &nbsp; &nbsp; pTo &nbsp; &nbsp;:= ToDir;<br>&nbsp; &nbsp; &nbsp; &nbsp; fFlags := FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;<br>&nbsp; &nbsp; &nbsp; &nbsp; fAnyOperationsAborted := False;<br>&nbsp; &nbsp; &nbsp; &nbsp; hNameMappings := nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; lpszProgressTitle := nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; if SHFileOperation(SHFileOpStruct) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RaiseLastWin32Error;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; FreeMem(ToDir, Length(AToDirectory)+2);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; FreeMem(FromDir, Length(AFromDirectory)+2);<br>&nbsp; end;<br>end;<br><br>procedure ToRecycle(AHandle: THandle; const ADirName: String);<br>var<br>&nbsp; SHFileOpStruct: TSHFileOpStruct;<br>&nbsp; DirName: PChar;<br>&nbsp; BufferSize: Cardinal;<br>begin<br>&nbsp; BufferSize := Length(ADirName) +1 +1;<br>&nbsp; GetMem(DirName, BufferSize);<br>&nbsp; try<br>&nbsp; &nbsp; FillChar(DirName^, BufferSize, 0);<br>&nbsp; &nbsp; StrCopy(DirName, PChar(ADirName));<br><br>&nbsp; &nbsp; with SHFileOpStruct do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Wnd := AHandle;<br>&nbsp; &nbsp; &nbsp; wFunc := FO_DELETE;<br>&nbsp; &nbsp; &nbsp; pFrom := DirName;<br>&nbsp; &nbsp; &nbsp; pTo := nil;<br>&nbsp; &nbsp; &nbsp; fFlags := FOF_ALLOWUNDO;<br><br>&nbsp; &nbsp; &nbsp; fAnyOperationsAborted := False;<br>&nbsp; &nbsp; &nbsp; hNameMappings := nil;<br>&nbsp; &nbsp; &nbsp; lpszProgressTitle := nil;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; if SHFileOperation(SHFileOpStruct) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; RaiseLastWin32Error;<br>&nbsp; finally<br>&nbsp; &nbsp; FreeMem(DirName, BufferSize);<br>&nbsp; end;<br>end;<br><br>procedure TMainForm.spbtnGetFromDirClick(Sender: TObject);<br>begin<br>&nbsp; edtFromDir.Text := GetDirectory;<br>end;<br><br>procedure TMainForm.spbtnGetToDirClick(Sender: TObject);<br>begin<br>&nbsp; edtToDir.Text := GetDirectory;<br>end;<br><br>procedure TMainForm.btnCopyDirectoryClick(Sender: TObject);<br>begin<br>&nbsp; CopyDirectoryTree(Handle, edtFromDir.Text, edtToDir.Text);<br>end;<br><br>procedure TMainForm.spbtnRecycleBinClick(Sender: TObject);<br>begin<br>&nbsp; edtRecycleDir.Text := GetDirectory;<br>end;<br><br>procedure TMainForm.btnRecycleDirClick(Sender: TObject);<br>begin<br>&nbsp; ToRecycle(0, edtRecycleDir.Text);<br>end;<br>
 
同意楼上,不过<br>&nbsp;pFrom:=PChar(s);<br>&nbsp; &nbsp; &nbsp; pTO:=PChar(t);很有可能要出问题,很多情况下会出错,<br>因为pTo和pFrom必须要用2个#0结尾!
 
那只有用递归的方法一个一个删除吧!<br>和查找文件一个道理!
 
SHFileOperation在Delphi的线程里面调用,会出现“无法读取源磁盘”的问题,<br>这种情况下只有自己用递归写一个了。
 
接受答案了.
 

Similar threads

后退
顶部