在DELPHI中如何实现拷贝目录!!!!!!!!!!!!!!!

CDMAA 2002-07-02 08:58:28
我想通过程序拷贝目录,因为我这个目录比较特别,但程序中我不大会,有人能告诉我吗?就像WINDOWS中的COPY再PASTE一样!
...全文
162 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
likevclinux 2002-07-11
  • 打赏
  • 举报
回复
来晚那
ahzhangcg 2002-07-10
  • 打赏
  • 举报
回复
Form6应为MainForm
ahzhangcg 2002-07-10
  • 打赏
  • 举报
回复
object Form6: TForm6
Left = 271
Top = 107
Width = 696
Height = 358
Caption = 'Form6'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object GroupBox1: TGroupBox
Left = 20
Top = 20
Width = 637
Height = 125
Caption = ' Copy directory example '
TabOrder = 0
object spbtnGetFromDir: TSpeedButton
Left = 590
Top = 20
Width = 23
Height = 22
Caption = '...'
end
object spbtnGetToDir: TSpeedButton
Left = 590
Top = 52
Width = 23
Height = 22
Caption = '...'
end
object edtFromDir: TEdit
Left = 40
Top = 20
Width = 553
Height = 21
TabOrder = 0
end
object edtToDir: TEdit
Left = 40
Top = 52
Width = 553
Height = 21
TabOrder = 1
end
object Button1: TButton
Left = 44
Top = 84
Width = 157
Height = 25
Caption = 'Copy Directory'
TabOrder = 2
end
end
object GroupBox2: TGroupBox
Left = 20
Top = 156
Width = 637
Height = 133
Caption = ' To recycle bin example '
TabOrder = 1
object spbtnRecycleBin: TSpeedButton
Left = 590
Top = 20
Width = 23
Height = 22
Caption = '...'
end
object edtRecycleDir: TEdit
Left = 40
Top = 20
Width = 553
Height = 21
TabOrder = 0
end
object btnRecycleDir: TButton
Left = 44
Top = 56
Width = 157
Height = 25
Caption = 'Delete Directory'
TabOrder = 1
end
end
object btnClose: TButton
Left = 312
Top = 296
Width = 75
Height = 25
Caption = 'Close'
TabOrder = 2
end
end
ahzhangcg 2002-07-10
  • 打赏
  • 举报
回复
unit MainFrm;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;

type
TMainForm = class(TForm)
GroupBox1: TGroupBox;
spbtnGetFromDir: TSpeedButton;
spbtnGetToDir: TSpeedButton;
edtFromDir: TEdit;
edtToDir: TEdit;
Button1: TButton;
GroupBox2: TGroupBox;
edtRecycleDir: TEdit;
spbtnRecycleBin: TSpeedButton;
btnRecycleDir: TButton;
btnClose: TButton;
procedure spbtnGetFromDirClick(Sender: TObject);
procedure spbtnGetToDirClick(Sender: TObject);
procedure btnCopyDirectoryClick(Sender: TObject);
procedure spbtnRecycleBinClick(Sender: TObject);
procedure btnRecycleDirClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation
uses ShellAPI, FileCtrl;
{$R *.DFM}

function GetDirectory: String;
begin
if not SelectDirectory(Result, [sdAllowCreate, sdPerformCreate, sdPrompt], 0) then
Result := EmptyStr;
end;

procedure CopyDirectoryTree(AHandle: THandle; const AFromDirectory, AToDirectory: String);
var
SHFileOpStruct: TSHFileOpStruct;
FromDir: PChar;
ToDir: PChar;
begin

GetMem(FromDir, Length(AFromDirectory)+2);
try
GetMem(ToDir, Length(AToDirectory)+2);
try

FillChar(FromDir^, Length(AFromDirectory)+2, 0);
FillChar(ToDir^, Length(AToDirectory)+2, 0);

StrCopy(FromDir, PChar(AFromDirectory));
StrCopy(ToDir, PChar(AToDirectory));

with SHFileOpStruct do
begin
Wnd := AHandle; // Assign the window handle
wFunc := FO_COPY; // Specify a file copy
pFrom := FromDir;
pTo := ToDir;
fFlags := FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted := False;
hNameMappings := nil;
lpszProgressTitle := nil;
if SHFileOperation(SHFileOpStruct) <> 0 then
RaiseLastWin32Error;
end;
finally
FreeMem(ToDir, Length(AToDirectory)+2);
end;
finally
FreeMem(FromDir, Length(AFromDirectory)+2);
end;
end;

procedure ToRecycle(AHandle: THandle; const ADirName: String);
var
SHFileOpStruct: TSHFileOpStruct;
DirName: PChar;
BufferSize: Cardinal;
begin
BufferSize := Length(ADirName) +1 +1;
GetMem(DirName, BufferSize);
try
FillChar(DirName^, BufferSize, 0);
StrCopy(DirName, PChar(ADirName));

with SHFileOpStruct do
begin
Wnd := AHandle;
wFunc := FO_DELETE;
pFrom := DirName;
pTo := nil;
fFlags := FOF_ALLOWUNDO;

fAnyOperationsAborted := False;
hNameMappings := nil;
lpszProgressTitle := nil;
end;

if SHFileOperation(SHFileOpStruct) <> 0 then
RaiseLastWin32Error;
finally
FreeMem(DirName, BufferSize);
end;
end;

procedure TMainForm.spbtnGetFromDirClick(Sender: TObject);
begin
edtFromDir.Text := GetDirectory;
end;

procedure TMainForm.spbtnGetToDirClick(Sender: TObject);
begin
edtToDir.Text := GetDirectory;
end;

procedure TMainForm.btnCopyDirectoryClick(Sender: TObject);
begin
CopyDirectoryTree(Handle, edtFromDir.Text, edtToDir.Text);
end;

procedure TMainForm.spbtnRecycleBinClick(Sender: TObject);
begin
edtRecycleDir.Text := GetDirectory;
end;

procedure TMainForm.btnRecycleDirClick(Sender: TObject);
begin
ToRecycle(0, edtRecycleDir.Text);
end;

procedure TMainForm.btnCloseClick(Sender: TObject);
begin
Close;
end;

end.
cqbonny 2002-07-10
  • 打赏
  • 举报
回复
我试试
I_am_zealot 2002-07-03
  • 打赏
  • 举报
回复
Performs a copy, move, rename, or delete operation on a file system object.

WINSHELLAPI int WINAPI SHFileOperation(LPSHFILEOPSTRUCT lpFileOp);
Parameters
lpFileOp:Pointer to an SHFILEOPSTRUCT structure that contains information the function needs to carry out the operation.

Return Values
Returns zero if successful or nonzero if an error occurs.

Now Supported on Windows NT
toplor 2002-07-03
  • 打赏
  • 举报
回复
提醒一下,请在uses部分加上ShellAPI单元


------------------------------
风过西窗客渡舟船无觅处
年年一川新草遥看却似旧
toplor 2002-07-03
  • 打赏
  • 举报
回复
这是完整的文件夹复制方法,看看吧:

procedure TForm1.CopyBtnClick(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,pchar(edit1.Text));//源目录
strpcopy(tobuf,pchar(edit2.Text));//目的目录
//填充opstruc记录
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
messagebox(handle,'复制完毕.','复制信息',mb_ok+mb_iconinformation);

end;

------------------------------
风过西窗客渡舟船无觅处
年年一川新草遥看却似旧
CDMAA 2002-07-02
  • 打赏
  • 举报
回复
能不给我详细的代码呀!!!
cs_str 2002-07-02
  • 打赏
  • 举报
回复
Shell 编程 也可以实现该功能
佣工7001 2002-07-02
  • 打赏
  • 举报
回复
用API
SHFileOperation()可实现Window一样的效果,支持回收站!~
CDMAA 2002-07-02
  • 打赏
  • 举报
回复
好像还是不行,不能把文件夹的特性拷过去,有没有实现WINDOW中的COPY方法的呀!
happyjoe 2002-07-02
  • 打赏
  • 举报
回复
搜索一下,前面有
cpls 2002-07-02
  • 打赏
  • 举报
回复
WinExec中执行'xcopy SourceDir TargetDir /s/e'
本书以详尽资料和大量的范例深入讨论了有关32位Windows编程的高级问题,包括进程和线程的管理、Win32的内存管理、消息处理、动态链接库、文件系统和设备输入输出、结构化异常处理、Unicode等,并重点讨论了Windows 95和Windows NT在实现上的不同之处,其包含了大量的编程技巧。 本书可供高等院校计算机专业的师生和广大的计算机编程人员使用。----------《Windows高级编程指南(第三版)》 原书: Advanced Windows (3rd Ed) 作者: Jeffrey Richter 译者: 王书洪 刘光明---------- 抽空将书的C程序用PASCAL写了一遍, 由于水平所限, 可能存在错误, 仅供参考! 感谢网友chinesexing提供PDF电子书! 感谢老朋友savetime兄提供附书代码! 刘麻子 于 05年7月7号----------附上代码列表: CopyData -- 利用WM_COPYDATA跨进程传递数据 VMStat -- 显示虚拟内存状态 VMAlloc -- 使用虚拟内存 (保留、提交、回收、释放) VMMap -- 遍历VMMap.exe进程的虚拟内存空间 DIPS & -- 进入Explorer.exe进程以操作桌面项目 DIPSLib SysInfo -- 显示系统相关信息 MMFShare -- 利用内存映射共享数据 FileRev -- 利用内存映射处理文件 Counter -- 纤程例子 (后台纤程以低优先级作运算) DiskInfo -- 显示本地逻辑驱动器信息 MultInst -- 使用EXE带有共享属性的PE节存储数据 TInjLib & -- 远程线程装载指定DLL到指定进程空间 ImgWalk LISLab -- 实验"局部输入状态" (Local Input State) TLSStat -- 在EXE模块使用静态TLS TLSDyn & -- 在DLL模块使用动态TLS SomeLib ModUse & -- 使用DLL带有共享属性的PE节存储数据 Module DocStats -- 利用事件对象使多个线程协同工作 (统计文档) Mutexes -- 利用互斥对象使(计算/显示)两个线程同步 DirWalk -- 遍历DirWalk.exe所在磁盘目录树 FileChng -- 监视目录变化 CritSecs -- 利用临界区对象使(计算/显示)两个线程同步 SprMrkt -- 利用互斥对象和信号量对象控制"超市"运转 SEHSum -- 利用SEH机制, 修复堆栈溢出 SEHTerm -- 演示SEH终止处理和异常抛出 SEHExcpt -- 利用SEH机制, 稀疏提交数组内存 SEHSoft -- 抛出自定义异常通知程序清零记录 Bucket -- SWMRG复合同步对象 (单写入/多读取) AlertIO -- 利用"回调通知"进行文件拷贝 (异步&分块) IOCmpPrt -- 利用"完成端口"进行文件拷贝 (异步&分块)

5,930

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧