你首先要获得写权限。具体你自己做,(呵呵,也可以给分让俺帮你) 正题: //拷贝整个目录下文件 function copydir(sdirname:pchar;stodirname:pchar):integer;stdcall; var hFindFile:Cardinal; //Cardinal的范围0..4294967295 t,tfile:shortstring; sCurDir:shortstring; FindFileData:WIN32_FIND_DATA; begin try //先保存当前目录 result:=1; sCurDir:=GetCurrentDir; ChDir(sDirName); hFindFile:=FindFirstFile('*.*',FindFileData); //FindFirstFile失败的返回值是 INVALID_HANDLE_VALUE if hFindFile<>INVALID_HANDLE_VALUE then begin if not DirectoryExists(sToDirName) then ForceDirectories(sToDirName); //如果目标目录不存在的话,则创建 repeat tfile:=string(FindFileData.cFileName); //WIN32_FIND_DATA 的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 CopyDir(pchar(string(sDirName)+'\'+tfile),pchar(string(t))) else CopyDir(pchar(string(sDirName)+tfile),pchar(string(sDirName)+tfile)); end //利用递归 else begin t:=sToDirName+'\'+tFile; CopyFile(pchar(string(tfile)),pchar(string(t)),True); end; until FindNextFile(hFindFile,FindFileData)=false; end else begin ChDir(sCurDir); result:=0; exit; end; //回到原来的目录下 ChDir(sCurDir); result:=1; except result:=0; end; end;//老兄,你要有进度条或再更好点的话,是否考虑多给点分,呵呵!
当然可以,我现在用了这个方法,但,我还想要其它的方法/ function CopyDirectory(const Source, Dest: string): boolean; var OpStruc: TSHFileOpStruct; frombuf, tobuf: Array [0..128] of Char; begin FillChar( frombuf, Sizeof(frombuf), 0 ); FillChar( tobuf, Sizeof(tobuf), 0 ); StrPCopy( frombuf, source ); StrPCopy( tobuf, dest ); With OpStruc DO Begin Wnd:= 0; wFunc:= FO_COPY; pFrom:= @frombuf; pTo:=@tobuf; fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION; fAnyOperationsAborted:= False; hNameMappings:= Nil; lpszProgressTitle:= Nil; end; ShFileOperation( OpStruc ); end;
正题:
//拷贝整个目录下文件
function copydir(sdirname:pchar;stodirname:pchar):integer;stdcall;
var
hFindFile:Cardinal;
//Cardinal的范围0..4294967295
t,tfile:shortstring;
sCurDir:shortstring;
FindFileData:WIN32_FIND_DATA;
begin
try //先保存当前目录
result:=1;
sCurDir:=GetCurrentDir;
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
//FindFirstFile失败的返回值是 INVALID_HANDLE_VALUE
if hFindFile<>INVALID_HANDLE_VALUE then
begin
if not DirectoryExists(sToDirName) then
ForceDirectories(sToDirName);
//如果目标目录不存在的话,则创建
repeat
tfile:=string(FindFileData.cFileName);
//WIN32_FIND_DATA 的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
CopyDir(pchar(string(sDirName)+'\'+tfile),pchar(string(t)))
else
CopyDir(pchar(string(sDirName)+tfile),pchar(string(sDirName)+tfile));
end //利用递归
else
begin
t:=sToDirName+'\'+tFile;
CopyFile(pchar(string(tfile)),pchar(string(t)),True);
end;
until FindNextFile(hFindFile,FindFileData)=false;
end
else
begin
ChDir(sCurDir);
result:=0;
exit;
end;
//回到原来的目录下
ChDir(sCurDir);
result:=1;
except
result:=0;
end;
end;//老兄,你要有进度条或再更好点的话,是否考虑多给点分,呵呵!
function CopyDirectory(const Source, Dest: string): boolean;
var
OpStruc: TSHFileOpStruct;
frombuf, tobuf: Array [0..128] of Char;
begin
FillChar( frombuf, Sizeof(frombuf), 0 );
FillChar( tobuf, Sizeof(tobuf), 0 );
StrPCopy( frombuf, source );
StrPCopy( tobuf, dest );
With OpStruc DO Begin
Wnd:= 0;
wFunc:= FO_COPY;
pFrom:= @frombuf;
pTo:=@tobuf;
fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:= False;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
ShFileOperation( OpStruc );
end;