程序:重启时强制删除/替换文件的工具

优质
小牛编辑
198浏览
2023-12-01
很简单的东西,绝对小巧绿色。勾选添加计划删除选项后,即使当前可删除/替换文件,也不进行操作,重启时再执行预定操作。

需要 InstallOptionsEx 插件 2.4.4 beta 2

引用PendFileOpt.nsi的内容:

/* 该脚本由 smilefly 制作
需要 InstallOptionsEx 插件 2.4.4 beta 2 (http://nsis.sourceforge.net/InstallOptionsEx)
请用蓝色网际的汉化增强版编译*/
; 安装程序初始定义常量
!define PRODUCT_NAME "重启时强制文件删除/替换工具"
!define PRODUCT_VERSION "2006.04.06"
!define PRODUCT_PUBLISHER "smilefly"
!define PRODUCT_WEB_SITE "http://smilefly.googlepages.com"
Name                      "${PRODUCT_NAME}"
OutFile                   "PendFileOpt.exe"
InstallDir                "$TEMP"
ShowInstDetails           show
SetCompressor             /SOLID lzma
BrandingText              "smilefly"
SpaceTexts                none
Caption                   "$(^NameDA)"
SubCaption                1 " "
InstallButtonText         "确定(O)"
ReserveFile               "Opt.ini"
; ------ MUI 现代界面定义 (1.67 版本以上兼容) ------
!include "MUI.nsh"
!include "UsefulLib.nsh"
Var HWND
; MUI 预定义常量
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\box-uninstall.ico"
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Header\1027.bmp"
Page custom SetOpt LeaveOpt
; 安装过程页面
!define MUI_PAGE_HEADER_TEXT "执行中..."
!define MUI_PAGE_HEADER_SUBTEXT "正在执行..."
!define MUI_INSTFILESPAGE_FINISHHEADER_TEXT "操作完成"
!define MUI_INSTFILESPAGE_FINISHHEADER_SUBTEXT "已成功完成所有操作"
!define MUI_INSTFILESPAGE_ABORTHEADER_TEXT "操作中止"
!define MUI_INSTFILESPAGE_ABORTHEADER_SUBTEXT "操作已被中止"
!insertmacro MUI_PAGE_INSTFILES
; 安装界面包含的语言设置
!insertmacro MUI_LANGUAGE "SimpChinese"

Section "MainSection" SEC01
   Clearerrors
   ReadINIStr $3 $R9 "Field 2" "State" ;$3: 原文件
   ReadINIStr $5 $R9 "Field 5" "State" ;$5: 备份开关
;备份
   ${If} $5 = 1
  IfFileExists "$3.bak" +7 0
  CopyFiles /SILENT $3 "$3.bak"
  IfFileExists "$3.bak" 0 +3
  DetailPrint "成功备份到 $3.bak"
  Goto +2
  DetailPrint "备份失败"
  Goto bakend
  ${For} $1 0 99
     IfFileExists "$3.bak$1" +7 0
     CopyFiles /SILENT $3 "$3.bak$1"
     IfFileExists "$3.bak$1" 0 +3
     DetailPrint "成功备份到 $3.bak$1"
     Goto +2
     DetailPrint "备份失败"
        ${ExitFor}
     ${Next}
     StrCmp $1 100 0 +2
  DetailPrint "出错"
   ${EndIf}
bakend:
   ReadINIStr $R4 $R9 "Field 4" "State" ;$R4: 计划删除开关
   ReadINIStr $8 "$R9" "Field 6" "State" ;$8: 替换/删除开关
   ${If} $8 = 1 ;替换
     ReadINIStr $4 $R9 "Field 8" "State" ;$4: 目标文件
     ${For} $1 0 99
           ClearErrors
     IfFileExists "$3$1" +11 0
        CopyFiles /SILENT $4 "$3$1"
        ${If} $R4 = 1
            Rename /REBOOTOK "$3$1" "$3"
           ${Else}
      Delete $3
      Rename "$3$1" "$3"
            IfErrors 0 +2
            Rename /REBOOTOK "$3$1" "$3"
        ${EndIf}
        ${ExitFor}
     ${Next}
     StrCmp $1 100 0 +2
  DetailPrint "出错"
     IfRebootFlag 0 +3
     DetailPrint "无法立即将 $3 替换为 $4, 将在重启后执行替换"
     Goto +2
     DetailPrint "成功将 $3 替换为 $4"
   ${Else} ;删除
     ${If} $R4 = 1
        FileOpen $R3 $3 r
      Delete /REBOOTOK "$3"
      FileClose $R3
     ${Else}
      Delete /REBOOTOK "$3"
     ${EndIf}
     IfRebootFlag 0 +3
     DetailPrint "无法立即删除 $3, 将在重启后删除"
     Goto +2
     DetailPrint "成功删除 $3"
   ${EndIf}
   Delete "$R9"
   IfRebootFlag 0 +3
   MessageBox MB_OK|MB_ICONEXCLAMATION "部分操作将在重启后执行, 请手动重启."
   DetailPrint "部分操作将在重启后执行, 请手动重启."
SectionEnd
Function SetOpt
!insertmacro MUI_HEADER_TEXT "文件强制删除/替换" "重启时强制文件删除/替换工具"
  InstallOptionsEx::initdialog /NOUNLOAD $R9
  Pop $HWND
  GetDlgItem $1 $HWND 1206
  EnableWindow $1 0
  GetDlgItem $1 $HWND 1207
  EnableWindow $1 0
  GetDlgItem $1 $HWND 1208
  EnableWindow $1 0
  InstallOptionsEx::show
FunctionEnd
Function LeaveOpt
    ReadINIStr $7 "$R9" "Settings" "State" ;$7 控件序号
    ReadINIStr $R7 "$R9" "Settings" "Notify" ;$R7:通知类型
    
    StrCmp $7 6 0 next ;是否重启后替换
    ReadINIStr $8 "$R9" "Field 6" "State"
    ${If} $8 = 1
  GetDlgItem $1 $HWND 1206
  EnableWindow $1 1
     GetDlgItem $1 $HWND 1207
     EnableWindow $1 1
     GetDlgItem $1 $HWND 1208
     EnableWindow $1 1
   ${Else}
  GetDlgItem $1 $HWND 1206
  EnableWindow $1 0
     GetDlgItem $1 $HWND 1207
     EnableWindow $1 0
     GetDlgItem $1 $HWND 1208
     EnableWindow $1 0
   ${EndIf}
   Abort
next:
StrCmp $7 10 0 next2 ;清除按钮
    StrCpy $0 0
loop:
    EnumRegValue $1 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager" $0
    StrCmp $1 "PendingFileRenameOperations" done
    StrCmp $1 "" non0
    IntOp $0 $0 + 1
    Goto loop
done:
    DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Session Manager" "PendingFileRenameOperations"
MessageBox MB_OK "已清除所有重启替换/删除项"
    Abort
non0:
    MessageBox MB_OK "无重启替换/删除项"
    Abort
next2:
StrCmp $R7 "ONNEXT" 0 next1 ;下一步时条件
    ReadINIStr $3 "$R9" "Field 2" "State"
    IfFileExists $3 +3 0
    MessageBox MB_OK "请选择一个要删除的文件"
    Abort
    ${If} $8 = 1
ReadINIStr $4 "$R9" "Field 8" "State"
    IfFileExists $4 +3 0
    MessageBox MB_OK "请选择用于替换的文件"
    Abort
    ${EndIf}
    Goto ends
next1:
Abort
ends:
FunctionEnd
Function .onInit
   InitPluginsDir
   GetTempFileName $R9
   File /oname=$R9 "Opt.ini"
FunctionEnd

;--------------------------------
;Callback
!define IDC_ABOUT 1300
!define IDC_LINK 1301
!define IDM_ABOUT 1
Function .onPageCallback
${If} $MSG = ${WM_INITDIALOG}
  Push $0
  Push $1
  Push $2
  Push $3
     ${GetDlgItemRect} $0 $1 $2 $3 $HWNDPARENT ${IDC_CANCEL}
  IntOp $0 $2 - $0
  IntOp $2 $3 - $1
  ${CreateButton} "关于(A)" 20 $1 $0 $2 $HWNDPARENT ${IDC_ABOUT}
  IntOp $3 $0 + 30
  IntOp $1 $1 + 3
  IntOp $2 $2 - 2
  ${CreateLink} $3 $1 130 $2 $HWNDPARENT ${IDC_LINK}
        ${GetSystemMenu} $0 $HWNDPARENT
     ${AppendMenu} $0 ${MF_SEPARATOR} 0 0
  ${AppendMenu} $0 ${MF_STRING} ${IDM_ABOUT} "关于(A)"
  Pop $3
  Pop $2
  Pop $1
  Pop $0
${ElseIf} $MSG = ${WM_COMMAND}
  ${If} $WPARAM = ${IDC_ABOUT}
  ${MessageBox2} `${PRODUCT_NAME}$\n版权所有 (C) ${PRODUCT_PUBLISHER}.$\n$\n我的主页:$\n${PRODUCT_WEB_SITE}` `关于` ${MB_OK}|${MB_ICONINFORMATION}
  ${ElseIf} $WPARAM = ${IDC_LINK}
      ExecShell open `${PRODUCT_WEB_SITE}`
  ${EndIf}
${ElseIf} $MSG = ${WM_DRAWITEM}
     ${DrawLink} `访问 ${PRODUCT_PUBLISHER} 的网站` ${IDC_LINK} $LPARAM
${ElseIf} $MSG = ${WM_SYSCOMMAND}
   ${If} $WPARAM = ${IDM_ABOUT}
      ${MessageBox2} `${PRODUCT_NAME}$\n版权所有 (C) ${PRODUCT_PUBLISHER}.$\n$\n我的主页:$\n${PRODUCT_WEB_SITE}` `关于` ${MB_OK}|${MB_ICONINFORMATION}
      ${EndIf}
${EndIf}
FunctionEnd

引用Opt.ini的内容:

; Ini file generated by the HM NIS Edit IO designer.
[Settings]
NumFields=10
[Field 1]
Type=Label
Text=要删除的文件:
Left=26
Right=82
Top=27
Bottom=35
[Field 2]
Type=Text
State=选择要删除的文件
Left=85
Right=218
Top=24
Bottom=36
ToolTipIcon=INFO
ToolTipTitle=源文件
ToolTipText=选择要删除的文件
ToolTipFlags=BALLOON
ToolTipTxtColor=0xCC9966
ToolTipBgColor=0xFFFFFF
ToolTipMaxWidth=300
[Field 3]
Type=Button
NOTIFY=OnClick
Flags=Open_FILEREQUEST|FILE_HIDEREADONLY|FILE_MUST_EXIST|FILE_EXPLORER
Filter=所有文件(*.*)
RefFields=2
Text=浏览(S)...
Left=224
Right=274
Top=22
Bottom=38
[Field 4]
Type=Checkbox
Text=计划删除(H)
Left=85
Right=149
Top=44
Bottom=54
ToolTipIcon=INFO
ToolTipTitle=计划删除
ToolTipText=勾选此处, 即使文件当前可被删除/替换, 也不执行操作, 重启后执行预定操作.
ToolTipFlags=BALLOON
ToolTipTxtColor=0xCC9966
ToolTipBgColor=0xFFFFFF
ToolTipMaxWidth=300
[Field 5]
Type=Checkbox
Text=创建备份(B)
Left=164
Right=230
Top=44
Bottom=54
ToolTipIcon=INFO
ToolTipTitle=创建源文件备份
ToolTipText=勾选此处创建源文件备份. 源文件将被备份为*.bak. 若已存在同名的文件, 则将备份为 *.bak0~*.bak99文件.
ToolTipFlags=BALLOON
ToolTipTxtColor=0xCC9966
ToolTipBgColor=0xFFFFFF
ToolTipMaxWidth=300
[Field 6]
Type=Checkbox
NOTIFY=OnClick
Text=文件替换(R)
Left=26
Right=91
Top=64
Bottom=73
ToolTipIcon=INFO
ToolTipTitle=替换文件
ToolTipText=勾选此处用目标文件替换源文件. 
ToolTipFlags=BALLOON
ToolTipTxtColor=0xCC9966
ToolTipBgColor=0xFFFFFF
ToolTipMaxWidth=300
[Field 7]
Type=Label
Text=用此文件替换:
Left=26
Right=82
Top=86
Bottom=94
[Field 8]
Type=Text
State=选择用于替换的文件
Left=85
Right=218
Top=83
Bottom=96
ToolTipIcon=INFO
ToolTipTitle=目标文件
ToolTipText=选择用于替换的文件
ToolTipFlags=BALLOON
ToolTipTxtColor=0xCC9966
ToolTipBgColor=0xFFFFFF
ToolTipMaxWidth=300
[Field 9]
Type=Button
NOTIFY=OnClick
Flags=Open_FILEREQUEST|FILE_HIDEREADONLY|FILE_MUST_EXIST
Filter=所有文件(*.*)
RefFields=8
Text=浏览(T)...
Left=224
Right=274
Top=81
Bottom=97
[Field 10]
Type=Button
NOTIFY=OnClick
Text="清除重启时删除 / 替换项(U)"
Left=120
Right=184
Top=108
Bottom=137