[Setup] AppName=My Program AppVerName=My Program version 1.5 DefaultDirName={pf}\My Program DisableProgramGroupPage=yes UninstallDisplayIcon={app}\MyProg.exe OutputDir=userdocs:Inno Setup Examples Output
[Files] ; Install our DLL to {app} so we can access it at uninstall time Source: "MyDll.dll"; DestDir: "{app}"
[Code] //Mode = False 备份文件到 {App}\Backup 文件夹下,保留原文件的相对路径 //Mode = True 还原备份文件到 {App} 文件夹下,保留原文件的相对路径 //DelFile = True 时,复制成功则删除原始文件 Function Backup(Files: TArrayOfString; DelFile: Boolean; Mode: Boolean): Boolean; var i,k,n: Integer; s: string; begin //获取 {App} 字符串长度 k := Length(ExpandConstant('{app}')); //获取 Files 列表项数 n := GetArrayLength(Files) - 1; //复制文件到目标文件夹 if Mode = False then begin for i := 0 to n do begin if FileExists(Files[i]) then s := Files[i]; Insert('\Backup',Files[i],k + 1); if not DirExists(Files[i]) then ForceDirectories(ExtractFileDir(Files[i])); if FileCopy(s,Files[i],False) then if DelFile = True then DeleteFile(s); end; end else begin for i := 0 to n do begin s := Files[i]; Insert('\Backup',Files[i],k + 1); if FileExists(Files[i]) then if FileCopy(Files[i],s,False) then if DelFile = True then DeleteFile(Files[i]); end; end end;
//当安装准备就绪时备份文件 procedure CurPageChanged(CurPageID: Integer); var Files: TArrayOfString; begin if CurPageID = wpReady then begin // 准备要备份的文件列表 SetArrayLength(Files,3) Files[0] := ExpandConstant('{app}\A.exe'); Files[1] := ExpandConstant('{app}\B.dll'); Files[2] := ExpandConstant('{app}\C.ocx'); Backup(Files,True,False); end; end;
//卸载时还原备份文件 procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep); Var Files: TArrayOfString; begin if CurUninstallStep = usUninstall then begin // 准备要备份的文件列表 SetArrayLength(Files,3) Files[0] := ExpandConstant('{app}\A.exe'); Files[1] := ExpandConstant('{app}\B.dll'); Files[2] := ExpandConstant('{app}\C.ocx'); Backup(Files,True,True); DelTree(ExpandConstant('{app}\Backup'), True, True, True); end; end;
wanfu
2016-10-13 23:24
上面的脚本太复杂,而且适用性有局限,改一下,可以自定义任意文件夹中的备份文件:
[Setup] AppName=My Program AppVerName=My Program version 1.5 DefaultDirName={pf}\My Program DisableProgramGroupPage=yes UninstallDisplayIcon={app}\MyProg.exe OutputDir=userdocs:Inno Setup Examples Output
[Files] ; Install our DLL to {app} so we can access it at uninstall time ; Source: "MyDll.dll"; DestDir: "{app}"
[Code] //备份文件 //复制 ExistFile 到 NewFile,保留时间戳和文件属性,成功返回 True,否则返回 False。 //FailIfExists = True 时,NewFile 存在时不覆盖,否则覆盖。 //DelFile = True 时,复制成功后删除 ExistFile 文件 Function Backup(ExistFile, NewFile: String; FailIfExists, DelFile: Boolean): Boolean; Var NewDir: string; begin if FileExists(ExistFile) then NewDir := ExtractFileDir(NewFile); //提取文件路径中的路径(不含文件名) if not DirExists(NewDir) then ForceDirectories(NewDir); //目标路径不存在时创建 Result := FileCopy(ExistFile,NewFile,FailIfExists); //复制文件 if Result then begin if DelFile = True then DeleteFile(ExistFile); //DelFile = True 时删除已备份的源文件 end; end;
//在安装准备就绪,实际安装开始之前备份文件 procedure CurStepChanged(CurStep: TSetupStep ); begin if CurStep = ssInstall then Backup(ExpandConstant('{app}\A.exe'),ExpandConstant('{app}\Backup\A.exe'),True,False); Backup(ExpandConstant('{app}\B.dll'),ExpandConstant('{app}\Backup\B.dll'),True,False); Backup(ExpandConstant('{app}\C.dll'),ExpandConstant('{app}\Backup\C.dll'),True,False); end;
//卸载时还原备份文件 procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep); begin if CurUninstallStep = usUninstall then Backup(ExpandConstant('{app}\Backup\A.exe'),ExpandConstant('{app}\A.exe'),False,True); Backup(ExpandConstant('{app}\Backup\B.dll'),ExpandConstant('{app}\B.dll'),False,True); Backup(ExpandConstant('{app}\Backup\C.dll'),ExpandConstant('{app}\C.dll'),False,True); DelTree(ExpandConstant('{app}\Backup'), True, True, True); //删除 Backup 文件夹及其子文件夹 end;
373723699
2016-12-01 15:17
wanfu:上面的脚本太复杂,而且适用性有局限,改一下,可以自定义任意文件夹中的备份文件:[Setup]AppName=My ProgramAppVerName=My Program version 1.5....... (2016-10-13 23:24)
如果仅要备份完整的文件夹,可以自己写段简单的代码来实现: [CODE] Procedure BackupFld(OldFldrName,NewFldrName:string); begin if DirExists(OldFldrName) then begin if not DirExists(NewFldrName) then begin RenameFile(OldFldrName,NewFldrName); CreateDir(OldFldrName); end else MsgBox('备份文件夹已存在!',mbError,mb_OK); end; end; 如果你还想把原文件夹里原部份文件保留过来,自己在中间加条复制命令就可以了。 BTW:如果你要备份的文件夹是在Program Files文件夹里,安装程序是需要管理员身份运行的,不然会失败。