将c:\abc复制到c:\temp的代码如下:uses ShellApi; procedure TForm1.Button3Click(Sender: TObject); var OpStruc: TSHFileOpStruct; frombuf, tobuf: Array [0..128] of Char; Begin FillChar( frombuf, Sizeof(frombuf), 0 ); FillChar( tobuf, Sizeof(tobuf), 0 ); StrPCopy( frombuf, 'c:\abc\*.*' ); StrPCopy( tobuf, 'c:\temp' ); With OpStruc DO Begin wFunc:= FO_COPY; pFrom:= @frombuf; pTo:=@tobuf; fFlags:= FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR; fAnyOperationsAborted:= False; hNameMappings:= Nil; lpszProgressTitle:= Nil; end; ShFileOperation( OpStruc ); end;
确实没找到 可以试试 function FindFirst(const FileName: String; var FindRec: TFindRec): Boolean; function FindNext(var FindRec: TFindRec): Boolean; procedure FindClose(var FindRec: TFindRec); 查找文件然后一个个的复制过去,如果遇到文件夹就递归调用。
楼上的老哥几位,楼长问的是在inno中,不是在delphi里
我说的就是Inno Setup。
function CopyFolder(const SourceFolderPath: String; DestinyFolderPath: String): Boolean; var strSrc: String; strDst: String; FindRec: TFindRec; FilesFound: Integer;begin //get first file strSrc := SourceFolderPath + '\*.*'; //strSrc := FindFirst(strSrc,FindRec); if (strSrc <> '.') and (strSrc <> '..') then begin // get the destiny and source paths strDst := DestinyFolderPath + '' + strSrc; //strSrc := strKfxTempPath + '' + strSrc; if FindFirst(ExpandConstant('{strSrc}\*'), FindRec) then begin try repeat if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then FilesFound := FilesFound + 1; until not FindNext(FindRec); finally FindClose(FindRec); end; end; MsgBox(IntToStr(FilesFound) + ' files found in the System directory.', mbInformation, MB_OK); end;
if not FileCopy(strSrc, strDst, false) then begin //erro on copy MsgBox('Erro on copy ' + strSrc + ' to ' + strDst, mbError, MB_OK); Result := false; exit; end;
//get next file //strSrc := FindNext; //return OK Result := true; end;
[Files] Source:"*"; DestDir: "{app}"
我要在code段实现,不要用files
function CopyDir(sDirName:String;sToDirName:String):Boolean; var t,tfile:String; FindRec: TFindRec; begin result:=true; if FindFirst(sDirName+'\*.*', FindRec) then begin if not DirExists(sToDirName) then ForceDirectories(sToDirName); try repeat tfile:=FindRec.name; if (tfile<>'.') and (tfile<>'..') then begin if FindRec.Attributes = FILE_ATTRIBUTE_DIRECTORY then begin t:=sToDirName+'\'+tfile; if not DirExists(t) then ForceDirectories(t); if sDirName[Length(sDirName)]<>'\' then CopyDir(sDirName+'\'+tfile,t) else CopyDir(sDirName+tfile,sToDirName+tfile); end else begin t:=sToDirName+'\'+tFile; FileCopy(sDirName+'\'+tfile,t,false); end end until not FindNext(FindRec); finally FindClose(FindRec); end; end else result:=false; end; 调用 CopyDir('D:\检验数据备份','C:\TestCopy');
function CopyDir(sDirName:String;sToDirName:String):Boolean; var t,tfile:String; FindRec: TFindRec; begin result:=true; if FindFirst(sDirName+'\*.*', FindRec) then begin if not DirExists(sToDirName) then ForceDirectories(sToDirName); try repeat tfile:=FindRec.name; if (tfile<>'.') and (tfile<>'..') then begin if FindRec.Attributes = FILE_ATTRIBUTE_DIRECTORY then begin t:=sToDirName+'\'+tfile; if not DirExists(t) then ForceDirectories(t); if sDirName[Length(sDirName)]<>'\' then CopyDir(sDirName+'\'+tfile,t) else CopyDir(sDirName+tfile,sToDirName+tfile); end else begin t:=sToDirName+'\'+tFile; FileCopy(sDirName+'\'+tfile,t,false); end end until not FindNext(FindRec); finally FindClose(FindRec); end; end else result:=false; end; 调用: CopyDir('D:\检验数据备份','c:\testcopy');
procedure TForm1.Button3Click(Sender: TObject);
var
OpStruc: TSHFileOpStruct;
frombuf, tobuf: Array [0..128] of Char;
Begin
FillChar( frombuf, Sizeof(frombuf), 0 );
FillChar( tobuf, Sizeof(tobuf), 0 );
StrPCopy( frombuf, 'c:\abc\*.*' );
StrPCopy( tobuf, 'c:\temp' );
With OpStruc DO Begin
wFunc:= FO_COPY;
pFrom:= @frombuf;
pTo:=@tobuf;
fFlags:= FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR;
fAnyOperationsAborted:= False;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
ShFileOperation( OpStruc );
end;
可以试试
function FindFirst(const FileName: String; var FindRec: TFindRec): Boolean;
function FindNext(var FindRec: TFindRec): Boolean;
procedure FindClose(var FindRec: TFindRec);
查找文件然后一个个的复制过去,如果遇到文件夹就递归调用。
我说的就是Inno Setup。
var
strSrc: String;
strDst: String;
FindRec: TFindRec;
FilesFound: Integer;begin
//get first file
strSrc := SourceFolderPath + '\*.*';
//strSrc := FindFirst(strSrc,FindRec);
if (strSrc <> '.') and (strSrc <> '..') then
begin // get the destiny and source paths
strDst := DestinyFolderPath + '' + strSrc;
//strSrc := strKfxTempPath + '' + strSrc;
if FindFirst(ExpandConstant('{strSrc}\*'), FindRec) then begin
try
repeat
if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
FilesFound := FilesFound + 1;
until not FindNext(FindRec); finally
FindClose(FindRec);
end;
end;
MsgBox(IntToStr(FilesFound) + ' files found in the System directory.',
mbInformation, MB_OK);
end;
if not FileCopy(strSrc, strDst, false) then
begin
//erro on copy
MsgBox('Erro on copy ' + strSrc + ' to ' + strDst, mbError, MB_OK);
Result := false;
exit;
end;
//get next file
//strSrc := FindNext; //return OK
Result := true;
end;
Source:"*"; DestDir: "{app}"
function CopyDir(sDirName:String;sToDirName:String):Boolean;
var
t,tfile:String;
FindRec: TFindRec;
begin
result:=true;
if FindFirst(sDirName+'\*.*', FindRec) then
begin
if not DirExists(sToDirName) then
ForceDirectories(sToDirName);
try
repeat
tfile:=FindRec.name;
if (tfile<>'.') and (tfile<>'..') then
begin
if FindRec.Attributes = FILE_ATTRIBUTE_DIRECTORY then
begin
t:=sToDirName+'\'+tfile;
if not DirExists(t) then
ForceDirectories(t);
if sDirName[Length(sDirName)]<>'\' then
CopyDir(sDirName+'\'+tfile,t)
else
CopyDir(sDirName+tfile,sToDirName+tfile);
end
else
begin
t:=sToDirName+'\'+tFile;
FileCopy(sDirName+'\'+tfile,t,false);
end
end
until not FindNext(FindRec);
finally
FindClose(FindRec);
end;
end
else
result:=false;
end;
调用
CopyDir('D:\检验数据备份','C:\TestCopy');
function CopyDir(sDirName:String;sToDirName:String):Boolean;
var
t,tfile:String;
FindRec: TFindRec;
begin
result:=true;
if FindFirst(sDirName+'\*.*', FindRec) then
begin
if not DirExists(sToDirName) then
ForceDirectories(sToDirName);
try
repeat
tfile:=FindRec.name;
if (tfile<>'.') and (tfile<>'..') then
begin
if FindRec.Attributes = FILE_ATTRIBUTE_DIRECTORY then
begin
t:=sToDirName+'\'+tfile;
if not DirExists(t) then
ForceDirectories(t);
if sDirName[Length(sDirName)]<>'\' then
CopyDir(sDirName+'\'+tfile,t)
else
CopyDir(sDirName+tfile,sToDirName+tfile);
end
else
begin
t:=sToDirName+'\'+tFile;
FileCopy(sDirName+'\'+tfile,t,false);
end
end
until not FindNext(FindRec);
finally
FindClose(FindRec);
end;
end
else
result:=false;
end;
调用:
CopyDir('D:\检验数据备份','c:\testcopy');