如何将一个目录下所有的文件拷贝到另一目录,谢谢(100分)

  • 主题发起人 主题发起人 randybj
  • 开始时间 开始时间
R

randybj

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将一个目录下所有的文件拷贝到另一目录,谢谢
 
ShFileOperation
 
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>procedure TMainForm.btnCloseClick(Sender: TObject);<br>begin<br>&nbsp; Close;<br>end;<br><br>end.<br>
 
《Delphi开发人员指南》中有相关的例子,不过较复杂。可以利用TFileListBox,<br>制定其目录即可取目录下所有文件(有投机取巧的嫌疑,不过可方便的解决问题)
 
uses ShellAPI;<br><br>....<br>ShellExecute('open', 'cmd.exe', 'copy c:/abc c:/def /e', '', sw_Normal);<br>....
 
各位老大,我用的是D6<br>ugvanxk的引用怎么使用 use what?<br><br>轻松虎 抱歉,参数类型不匹配<br>谢谢各位,能否再详细些
 
同意“轻松虎”
 
ShellExecute(handle,'open', 'cmd.exe', 'copy c:/abc c:/def /e', '', sw_Normal);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^<br>少了一个 Handle
 
多人接受答案了。
 
后退
顶部