delphi 中如何拷贝文件和整个文件夹(50分)

Z

zhiping

Unregistered / Unconfirmed
GUEST, unregistred user!
我编程要拷贝文件或文件夹.delphi中没有现成函数.怎么办?<br>若需调用api函数是那些?
 
有现成的函数: copyfile
 
呵呵, 用winexec或shellexecute可以.<br>另外, 调用SHFileOperation(LPSHFILEOPSTRUCT lpFileOp)更好, 还可以出现<br>文件copy的动画:)<br>typedef struct _SHFILEOPSTRUCT{<br>&nbsp; &nbsp; &nbsp;HWND &nbsp; &nbsp; &nbsp; &nbsp; hwnd;<br>&nbsp; &nbsp; &nbsp;UINT &nbsp; &nbsp; &nbsp; &nbsp; wFunc; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; pFrom;<br>&nbsp; &nbsp; &nbsp;LPCSTR &nbsp; &nbsp; &nbsp; pTo;<br>&nbsp; &nbsp; &nbsp;FILEOP_FLAGS fFlags; <br>&nbsp; &nbsp; BOOL &nbsp; &nbsp; &nbsp; &nbsp; fAnyOperationsAborted;<br>&nbsp; &nbsp; &nbsp;LPVOID &nbsp; &nbsp; &nbsp; hNameMappings; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; lpszProgressTitle; <br>} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT;
 
procedure copydir (root: string,destdir:string);<br>var<br>&nbsp; test_name: string;<br>&nbsp; full_name: string;<br>&nbsp; s: TSearchRec;<br>begin<br>test_name:='*.*';<br>&nbsp; if stop_requested then Exit;<br>&nbsp; root := LowerCase (root);<br>&nbsp; if root [Length (root)] &lt;&gt; '/' then<br>&nbsp; &nbsp; &nbsp;root := root + '/';<br>// &nbsp; &nbsp; &nbsp;Application.ProcessMessages;<br>&nbsp; if FindFirst (test_name, faAnyFile, s) = 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;repeat<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if stop_requested then Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;with s do begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name := LowerCase (Name);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Attr &lt;&gt; faDirectory) then &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; full_name := root + Name;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copyfile(full_name,destdir+name)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;until FindNext (s) &lt;&gt; 0;<br>&nbsp; &nbsp; &nbsp; FindClose (s);<br>&nbsp; end;<br>&nbsp; test_name := root + '*.*';<br>&nbsp; if FindFirst (test_name, faAnyFile, s) = 0 then<br>&nbsp; &nbsp; &nbsp;repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;with s do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((Attr and faDirectory) &lt;&gt; 0) and ((Name &lt;&gt; '.') and (Name &lt;&gt; '..'))<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then copydir (root + Name,destdir+Name);<br>&nbsp; &nbsp; &nbsp;until FindNext (s) &lt;&gt; 0;<br>&nbsp; FindClose (s);<br>end;<br>
 
Delphi有自带的例子,参见 <br>C:/Program Files/Borland/Delphi4/Demos/Doc/Filmanex
 
唉,已答问题有几个了。<br>http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=119086<br>http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=92662<br><br>什么都有了:xcopy deltree...
 
拷贝文件 copyfile('c:/xxxx','c:/xxxx',false);<br>
 
怎样拷贝目录 <br>How to Copy A Directory?<br>In Win32 there is an API function for that: SHFileOperation.<br><br>copy a whole directory tree<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; OpStruc: TSHFileOpStruct;<br>&nbsp; frombuf, tobuf: Array [0..128] of Char;<br>Begin<br>&nbsp; FillChar( frombuf, Sizeof(frombuf), 0 );<br>&nbsp; FillChar( tobuf, Sizeof(tobuf), 0 );<br>&nbsp; StrPCopy( frombuf, 'd:/brief/*.*' );<br>&nbsp; StrPCopy( tobuf, 'd:/temp/brief' );<br>&nbsp; With OpStruc DO Begin<br>&nbsp; &nbsp; Wnd:= Handle;<br>&nbsp; &nbsp; wFunc:= FO_COPY;<br>&nbsp; &nbsp; pFrom:= @frombuf;<br>&nbsp; &nbsp; pTo:=@tobuf;<br>&nbsp; &nbsp; fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;<br>&nbsp; &nbsp; fAnyOperationsAborted:= False;<br>&nbsp; &nbsp; hNameMappings:= Nil;<br>&nbsp; &nbsp; lpszProgressTitle:= Nil;<br>&nbsp; end;<br>&nbsp; ShFileOperation( OpStruc );<br>end;<br><br>&nbsp;<br>
 
我用以下代码,效果很好.<br>procedure mycopyfile(s,d:string);<br>var<br>&nbsp; &nbsp;sr,temp:tsearchrec;<br>begin<br><br>&nbsp; if findfirst(s+'/*.*',faanyfile,sr)=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; if (sr.name&lt;&gt; '.')and(sr.name&lt;&gt;'..') then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;if sr.Attr=fadirectory then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if findfirst(d+'/'+sr.name,faanyfile,temp)=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;createdirectory(pchar(d+'/'+sr.name),lps);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mycopyfile(s+'/'+sr.name,d+'/'+sr.name);<br>&nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;copyfile(pchar(s+'/'+sr.name),pchar(d+'/'+sr.name),false);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; while findnext(sr)=0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; if (sr.name&lt;&gt; '.')and(sr.name&lt;&gt;'..') then<br>&nbsp; &nbsp; &nbsp; &nbsp;if sr.Attr=fadirectory then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if findfirst(PCHAR(d+'/'+sr.name),fadirectory,temp)&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;createdirectory(pchar(d+'/'+sr.name),lps);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp:=sr;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mycopyfile(s+'/'+sr.name,d+'/'+sr.name);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sr:=temp;<br>&nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; copyfile(pchar(s+'/'+sr.name),pchar(d+'/'+sr.name),false);<br>&nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; findclose(sr);<br>&nbsp; end;<br>end;
 
用个最简单的办法:<br>winexec('command.com /c copy *.* c:/aa',sw_normal);<br>winexec('command.com /c xcopy ......
 
windows下出现DOS的黑屏不专业。
 
接受答案
 
顶部