急急急急急!!!关于文件的copy(50分)

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

royal666

Unregistered / Unconfirmed
GUEST, unregistred user!
请问大家,我现在是将一个文件拷贝到另一个文件下,程序如下,怎样做我才能实现:<br>1.当源文件是空时,有一对话框显示“ 源文件是空 !”<br>2.当目标文件所在磁盘的空间小于源文件时显示“磁盘空间不足”<br>3.当与源文件重名时,显示“重名,是否改名继续?”<br>4.当拷贝完成时显示对话框“copy成功!”<br><br>Function TForm1.Copydirectory(Source:string;pDirectory:string):boolean;<br>//&amp;Auml;&amp;iquest;&amp;Acirc;&amp;frac14;&amp;micro;&amp;Auml;&amp;iquest;&amp;frac12;±&amp;acute;<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, Source);<br>&nbsp; StrPCopy( tobuf, pdirectory);<br>&nbsp; if not directoryexists(pdirectory) then<br>&nbsp; &nbsp; &nbsp; &nbsp; if not createdir(pdirectory) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showmessage('&amp;sup2;&amp;raquo;&amp;Auml;&amp;Uuml;&amp;acute;&amp;acute;&amp;frac12;¨&amp;Auml;&amp;iquest;&amp;Acirc;&amp;frac14;'+pdirectory+chr(13)+chr(10)+'&amp;raquo;ò&amp;Icirc;&amp;THORN;&amp;cedil;&amp;Atilde;&amp;Auml;&amp;iquest;&amp;Acirc;&amp;frac14;&amp;Egrave;¨&amp;Iuml;&amp;THORN;');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; With OpStruc DO Begin<br>&nbsp; &nbsp; Wnd:= 0;<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 or fof_filesonly;<br>&nbsp; &nbsp; fAnyOperationsAborted:= false;<br>&nbsp; &nbsp; hNameMappings:= Nil;<br>&nbsp; &nbsp; lpszProgressTitle:=PChar('&amp;Otilde;&amp;yacute;&amp;Ocirc;&amp;Uacute;&amp;cedil;&amp;acute;&amp;Ouml;&amp;AElig;&amp;Icirc;&amp;Auml;&amp;frac14;&amp;thorn;&amp;frac14;&amp;ETH;'+Source+'&amp;micro;&amp;frac12;'+pDirectory);<br>&nbsp; end;<br>&nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; ShFileOperation(OpStruc );<br>&nbsp; &nbsp; &nbsp; &nbsp; CopyDirectory:=true;<br><br>&nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp;CopyDirectory:=false;<br><br>&nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; end;<br>end;<br>谢谢大家!!!<br>
 
Function CopyFile(SrcFile:String;DstFile:String):Boolean;<br>var<br>&nbsp;f:FILE;<br>&nbsp;fsize:longint;<br>&nbsp;diskfree:longint; <br>begin<br>&nbsp; &nbsp; &nbsp; if not FileExists(DstFile) then ShowMessage('file is nil')<br>&nbsp; &nbsp; &nbsp; else if messagebox(0,'file exist,overwrite?','ask',mb_yesno) = idno then exit;<br>&nbsp; &nbsp; &nbsp; //get file size<br>&nbsp; &nbsp; &nbsp; AssignFile(f,SrcFile);<br>&nbsp; &nbsp; &nbsp; reset(f); <br>&nbsp; &nbsp; &nbsp; fsize := fielsize(f);<br>&nbsp; &nbsp; &nbsp; closefile(f);<br>&nbsp; &nbsp; &nbsp; diskfree := diskfree(0);//这里要处理到目标盘<br>&nbsp; &nbsp; &nbsp; if filesize &gt;diskfree then showmessage('disk full');<br>&nbsp; &nbsp; &nbsp; if copyfile(SrcFile,DstFile,false) then showmessage('copy file suc')<br>&nbsp; &nbsp; &nbsp; else showmessage('copy file fail');<br>end;<br>&nbsp; &nbsp; &nbsp;
 
我是希望能用ShFileOperation来完成,可行吗?<br>神阿,救救我!
 
用 fileexists 和 CopyFile不就行了。不需要用 ShFileOperation<br>在 ShFileOperation 中文件名后要加两个 char(0)
 
就是象windows的资源管理器实现的功能一样,所以我觉得用ShFileOperation可以来完成,可具体是<br>怎么实现呢?急急急急!!!
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1258708
 
大家都能说的详细些马?再次谢过了
 
麻烦大家了,我真的是比较愚钝,能不能把源码写出来(最好能带注释的)(我接触delphi不久)?谢谢!!<br>最好是用shellapi,但我也想知道delphi的实现方法,再次感谢!!!
 
真没有人可以帮我吗?大富翁的高手都不屑回答此问题吗?
 
FileCopy()
 
这个问题该结束了吧
 
怎样实现1.当源文件是空时,有一对话框显示“ 源文件是空 !”<br>2.当目标文件所在磁盘的空间小于源文件时显示“磁盘空间不足”<br>3.当与源文件重名时,显示“重名,是否改名继续?”<br>4.当拷贝完成时显示对话框“copy成功!”??????<br>我还是一头雾水。<br>我知道我很笨,可大家不都是一步步来的吗?
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; s,s2:string;<br>&nbsp; fs:TFileStream;<br>&nbsp; n:integer;<br>begin<br>&nbsp; s:='c:/windows/notepad.exe';<br>&nbsp; s2:='d:/bb.txt';<br>&nbsp; if not fileexists(s) then<br>&nbsp; begin<br>&nbsp; &nbsp; showmessage(Format('文件“%s”不存在!',));<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; fs:=TFileStream.Create (s,fmOpenRead);<br>&nbsp; n:=fs.size;<br>&nbsp; fs.free;<br>&nbsp; if n =0 then<br>&nbsp; begin<br>&nbsp; &nbsp; showmessage(Format('文件“%s”是空的!',));<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; if fileexists(s2) then<br>&nbsp; &nbsp; if (messagebox(handle,pchar('文件“'+s2+'”已存在,是否覆盖?'),<br>&nbsp; &nbsp; &nbsp; pchar('文件已存在'),MB_YESNO + MB_DEFBUTTON1)=IDNo)<br>&nbsp; &nbsp; then &nbsp;exit;<br><br>&nbsp; if not copyfile(pchar(s),pchar(s2),false) then<br>&nbsp; &nbsp; &nbsp;showmessage('文件拷贝失败,请检查磁盘空间。');<br>end;<br>
 
那如何判断磁盘空间不足呢?<br>
 
怎么要这么详细?显示是不是成功就行了?<br><br>GetDiskFreeSpace<br>The GetDiskFreeSpace function retrieves information about the specified disk, including the amount of free space on the disk. <br><br>This function has been superseded by the GetDiskFreeSpaceEx function. New Win32-based applications should use GetDiskFreeSpaceEx. <br><br>BOOL GetDiskFreeSpace(<br>&nbsp; LPCTSTR lpRootPathName, &nbsp; &nbsp;// pointer to root path<br>&nbsp; LPDWORD lpSectorsPerCluster, &nbsp;// pointer to sectors per cluster<br>&nbsp; LPDWORD lpBytesPerSector, &nbsp;// pointer to bytes per sector<br>&nbsp; LPDWORD lpNumberOfFreeClusters,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pointer to number of free clusters<br>&nbsp; LPDWORD lpTotalNumberOfClusters <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pointer to total number of clusters<br>);<br>&nbsp;<br>Parameters<br>lpRootPathName <br>Pointer to a null-terminated string that specifies the root directory of the disk to return information about. If lpRootPathName is NULL, the function uses the root of the current directory. If this parameter is a UNC name, you must follow it with an additional backslash. For example, you would specify //MyServer/MyShare as //MyServer/MyShare/. <br>Windows 95: The initial release of Windows 95 does not support UNC paths for the lpszRootPathName parameter. To query the free disk space using a UNC path, temporarily map the UNC path to a drive letter, query the free disk space on the drive, then remove the temporary mapping. Windows 95 OSR2 and later: UNC paths are supported. <br><br>lpSectorsPerCluster <br>Pointer to a variable for the number of sectors per cluster. <br>lpBytesPerSector <br>Pointer to a variable for the number of bytes per sector. <br>lpNumberOfFreeClusters <br>Pointer to a variable for the total number of free clusters on the disk that are available to the user associated with the calling thread. <br>Windows NT 5.0 and later: If per-user disk quotas are in use, this value may be less than the total number of free clusters on the disk. <br><br>lpTotalNumberOfClusters <br>Pointer to a variable for the total number of clusters on the disk that are available to the user associated with the calling thread. <br>Windows NT 5.0 and later: If per-user disk quotas are in use, this value may be less than the total number of clusters on the disk. <br><br>Return Values<br>If the function succeeds, the return value is nonzero.<br><br>If the function fails, the return value is zero. To get extended error information, call GetLastError. <br><br>Remarks<br>The GetDiskFreeSpaceEx function lets you avoid the arithmetic required by the GetDiskFreeSpace function. <br><br>Windows 95: <br>The GetDiskFreeSpace function returns incorrect values for volumes that are larger than 2 gigabytes. The function caps the values stored into *lpNumberOfFreeClusters and *lpTotalNumberOfClusters so as to never report volume sizes that are greater than 2 gigabytes. <br>Even on volumes that are smaller than 2 gigabytes, the values stored into *lpSectorsPerCluster, *lpNumberOfFreeClusters, and *lpTotalNumberOfClusters values may be incorrect. That is because the operating system manipulates the values so that computations with them yield the correct volume size. <br><br>
 
jxscd;用你的程序调试,不知为什么每次总是说文件不存在
 
上面的代码还不够清楚吗?<br><br><br>&nbsp; s:='c:/windows/notepad.exe'; &nbsp;//源文件<br>&nbsp; s2:='d:/bb.txt'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //目标文件
 
对不起时我没说清楚<br>应该是目录的拷贝,流对目录没法操作
 
目录的拷贝我不是叫你到下面的地址去看嘛:<br><br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=1258708
 
多人接受答案了。
 
顶部