怎样用ShFileOpStruct拷贝多个文件到多个目录 ,pfrom参数可以装多少个文件名,有无限制? (100分)

  • 主题发起人 主题发起人 nutian
  • 开始时间 开始时间
N

nutian

Unregistered / Unconfirmed
GUEST, unregistred user!
需要拷贝的文件(包含路径)已经列在StringList中,怎么用ShFileOpStruct结构把这些文件拷贝到指定的不同的目录?
 
http://cnpack.cosoft.org.cn/download/JCL.zip<br>自己看吧
 
用控件不能提高自己,也不想花时间研究具体哪个控件!
 
老大,那不是都有源码吗?<br>那是JEDI的公用开发库,你看 source/ 下的 file相关就可以了<br>那里不只有拷贝,还有删除、移动<br>可怜我一片好心
 
function FileAction(fFROM, fTO: string; Action: integer): boolean;<br>var<br>&nbsp; FData: TShFileOpStruct;<br>begin<br>&nbsp; fillchar(FData, sizeof(TShFileOpStruct), 0);<br>&nbsp; Fdata.pFrom := PChar(fFrom + #0#0);<br>&nbsp; //设立数据源路径和文件、目录名,支(?,*通配符)<br>&nbsp; fdata.pTo := PChar(fTo + #0#0); //设立目标路径<br>&nbsp; case Action of<br>&nbsp; &nbsp; 0: fdata.wFunc := FO_COPY; //copy<br>&nbsp; &nbsp; 1: fdata.wFunc := FO_MOVE; //move<br>&nbsp; &nbsp; 2: fdata.wFunc := FO_DELETE; //delete<br>&nbsp; &nbsp; 3: fdata.wFunc := FO_RENAME; //rename<br>&nbsp; else<br>&nbsp; &nbsp; fdata.wFunc := FO_COPY;<br>&nbsp; end;<br>&nbsp; fData.fFlags := FOF_NOCONFIRMATION or FOF_SILENT; //设定操作选 项<br>&nbsp; fData.fAnyOperationsAborted := false;<br>&nbsp; fData.hNameMappings := nil;<br>&nbsp; fData.lpszProgressTitle := nil;<br>&nbsp; result := ShFileOperation(FData) = 0;<br>end;
 
to 一只笨小狗:<br>&nbsp; &nbsp;我是多个文件,拷到多个目录,数据源列在stringlist里,pfrom怎么设?<br><br>pfrom作用如下:<br>pFrom:指定一个或多个源文件名的缓冲区地址。多个名字必须用NULL分隔。名字列表必须用两个NULL(nil,'/0')来结束。<br>怎么用stringlist来设定pfrom?pfrom可以装多少个文件名,有无限制?<br>
 
我要拷贝d:/a/1.exe 和d:/a/2.exe 到c:/b/, &nbsp;pfrom这样设置成功,<br>F.pFrom := PChar(SourceName+#0+'d:/a/2.exe'+#0#0);<br>F.pTo := PChar('c:/b/+#0#0);<br>但如果文件上千个,pfrom里面能不能装下?会不会溢出?<br><br>
 
当然不会了,放心期间就分批吧。<br>或者用*.exe或者其他。
 
复制目录:<br>///复制Source整个目录到DEST目录,如果Dest不存在,自动建立,如果DEST存在,那么Source将作为Dest的子目录!<br>//例如如果要复制E:/Temp整个目录到E:/那么代码为: copydirectory('e:/temp','e:/');<br>///如果要复制E:/Temp到E:/Test目录下面,那么代码为:CopyDirecotry('E:/Temp','E:/TEST');<br>function CopyDirectory(const Source, Dest: string): boolean;<br>var<br>&nbsp; fo: TSHFILEOPSTRUCT;<br>begin<br>&nbsp; FillChar(fo, SizeOf(fo), 0);<br>&nbsp; with fo do<br>&nbsp; begin<br>&nbsp; &nbsp; Wnd := 0;<br>&nbsp; &nbsp; wFunc := FO_COPY;<br>&nbsp; &nbsp; pFrom := PChar(source+#0);<br>&nbsp; &nbsp; pTo := PChar(Dest+#0);<br>&nbsp; &nbsp; fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR &nbsp; &nbsp;;<br>&nbsp; end;<br>&nbsp; Result := (SHFileOperation(fo) = 0);<br>end;<br><br>//只要两个目录就可以操作了<br>//你可以测试一下容量 一般不会了
 
自己搞定,用了2千个文件测试,全部装入pfrom中,SHFileOperation复制正常,看来不用担心容量<br><br>function CopyFile(SourceName, TargetName: string): Boolean;<br>var<br>&nbsp; F: TShFileOpStruct;<br>&nbsp; Index: Integer;<br>&nbsp; Fromdir,Todir,ToFile:string;<br>begin<br>&nbsp; for Index := 0 to namelist.Count - 1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Fromdir := fromdir+namelist[index]+#0;//从namelist中取出文件名,用#0隔开<br>&nbsp; &nbsp; &nbsp; ToFile :=TargetName+ copy(namelist[index],length(SourceName)+1,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;length(namelist[index])-length(SourceName));//转换为目标路径<br>&nbsp; &nbsp; &nbsp; Todir :=Todir+ToFile+#0;<br>&nbsp; &nbsp; end;<br>&nbsp; F.wnd := Form1.Handle;<br>&nbsp; F.wFunc := FO_COPY; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {操作方式}<br>&nbsp; F.pFrom := PChar(fromdir+#0);<br>&nbsp; F.pTo := PChar(Todir+ #0);<br>&nbsp; <br>&nbsp; F.fFlags:= FOF_NOCONFIRMATION or FOF_MULTIDESTFILES &nbsp; &nbsp;<br>&nbsp; F.fAnyOperationsAborted:= true;<br>&nbsp; if ShFileOperation(F)&lt;&gt;0 then showmessage('文件没复制完');<br>&nbsp; if IOResult&lt;&gt;0 &nbsp;then showmessage('文件没复制完');<br><br>&nbsp; end;<br>==============================================<br>namelist为函数外部定义的一个全局变量,里面装的是需拷贝的文件列表,定义如下<br>var namelist:Tstringlist;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部