如何实现copy功能?(50分)

  • 主题发起人 主题发起人 wuyuhuai
  • 开始时间 开始时间
W

wuyuhuai

Unregistered / Unconfirmed
GUEST, unregistred user!
比如我想在程序里实现自动copy某个文件到特定地方
比如说我希望将c:/windows/目录下的A.EXE copy到d:/temp/目录下
程序改如何实现?
 
用 copyfile()
遇到问题,要养成先查查帮助的习惯
The CopyFile function copies an existing file to a new file.

BOOL CopyFile(

LPCTSTR lpExistingFileName, // address of name of an existing file
LPCTSTR lpNewFileName, // address of filename to copy to
BOOL bFailIfExists // flag for operation if file exists
);
Parameters

lpExistingFileName

Points to a null-terminated string that specifies the name of an existing file.

lpNewFileName

Points to a null-terminated string that specifies the name of the new file.

bFailIfExists

Specifies how this operation is to proceed if a file of the same name as
that specified by lpNewFileName already exists. If this parameter is TRUE
and the new file already exists, the function fails. If this parameter is
FALSE and the new file already exists, the function overwrites the existing
file and succeeds.

Return Value

If the function succeeds, the return value is TRUE.
If the function fails, the return value is FALSE. To get extended error
information, call GetLastError.
 
hubdog 上的答案 粘贴过来如下

---- 1、拷贝目录

---- 为了能拷贝目录下带有子目录的情况,先定义一个辅助的拷贝函数,它是递归执行的,直到把目录下的所有文件和子目录都拷贝完。

---- 1.1拷贝目录的递归辅助函数:DoCopyDir

function DoCopyDir(sDirName:String;
sToDirName:String):Boolean;
var
hFindFile:Cardinal;
t,tfile:String;
sCurDir:String[255];
FindFileData:WIN32_FIND_DATA;
begin
//先保存当前目录
sCurDir:=GetCurrentDir;
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile< >INVALID_HANDLE_VALUE then
begin
if not DirectoryExists(sToDirName) then
ForceDirectories(sToDirName);
repeat
tfile:=FindFileData.cFileName;
if (tfile='.') or (tfile='..') then
Continue;
if FindFileData.dwFileAttributes=
FILE_ATTRIBUTE_DIRECTORY then
begin
t:=sToDirName+'/'+tfile;
if not DirectoryExists(t) then
ForceDirectories(t);
if sDirName[Length(sDirName)]< >'/' then
DoCopyDir(sDirName+'/'+tfile,t)
else
DoCopyDir(sDirName+tfile,sToDirName+tfile);
end
else
begin
t:=sToDirName+'/'+tFile;
CopyFile(PChar(tfile),PChar(t),True);
end;
until FindNextFile(hFindFile,FindFileData)=false;
FindClose(hFindFile);
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
//回到原来的目录下
ChDir(sCurDir);
result:=true;
end;

---- 1.2拷贝目录的函数:CopyDir

function CopyDir(sDirName:String;
sToDirName:string):Boolean;
begin
if Length(sDirName)< =0 then
exit;
//拷贝...
Result:=DoCopyDir(sDirName,sToDirName);
end;

 
多谢两位大哥
呵呵,我的习惯是不大好
不大愿意自己去查
喜欢直接问别人,得到直接的答案
可能是懒人一个
^_^
多谢
 

Similar threads

回复
0
查看
999
不得闲
回复
0
查看
708
不得闲
回复
0
查看
825
不得闲
后退
顶部