/*Kawvin批量重命名工具
    程序: Kawvin批量重命名工具
    作用: 批量重命名工具
    作者: Kawvin
    版本: 0.2
    使用方法:
    (1)见操作界面
    (2)支持参数发送,可做为QZ插件,QZ的参数: {file:"[P]";}
*/

Label_ScriptSetting:	;{ 脚本前参数设置
  #SingleInstance force
;}

Label_DefVar:	;{ 设置主参数
  AppName=Kawvin批量重命名工具
  AppVer=0.2
  HS:=35
  X1:=20
  Y1:=10, Y11:=Y1+5
  Y2:=Y1+HS, Y22:=Y2+5
  Y3:=Y2+HS, Y33:=Y3+5
  Y4:=Y3+HS, Y44:=Y4+5
  Y5:=Y4+HS, Y55:=Y5+5
  Y6:=Y5+HS, Y66:=Y6+5
  Y7:=Y6+HS, Y77:=Y7+5
  Y8:=Y7+HS, Y88:=Y8+5
;}

Label_DrawGUI:	;{ 绘制窗体
  Gui Add, Radio, section x%X1% y%Y1% w50 h21 vRunID gPreview,替换
  Gui Add, Radio,  x%X1% y%Y2% w50 h21 gPreview,替换
  Gui Add, Radio,  x%X1% y%Y3%  w50 h21 gPreview,替换
  Gui Add, Radio,  x%X1% y%Y4%  w50 h21 gPreview,插入
  Gui Add, Radio,  x%X1% y%Y5%  w50 h21 gPreview,插入
  Gui Add, Radio,  x%X1% y%Y6%  w50 h21 gPreview,删除
  Gui Add, Radio,  x%X1% y%Y7%  w50 h21 gPreview,删除
  
  Gui Add, Text, section y%Y11% w25 h21,替换
  Gui Add, Edit, ys-3 w130 h21 +Center vRep1_Old gPreview,
  Gui Add, Text, ys w15 h21,为
  Gui Add, Edit, ys-3 w140 h21 +Center vRep1_New gPreview,
  Gui Add, Text, section  xs y%Y22% w60 h21,替换正数第
  Gui Add, Edit, ys-3 w40 h21 +Center vRep2_Num gPreview,
  Gui Add, UpDown, Range1-255
  Gui Add, Text, ys-3 w15 h21,个
  Gui Add, Edit, ys-3 w80 h21 +Center vRep2_Old gPreview,
  Gui Add, Text, ys w15 h21,为
  Gui Add, Edit, ys-3 w80 h21 +Center vRep2_New gPreview,
  Gui Add, Text, section xs y%Y33% w60 h21,替换倒数第
  Gui Add, Edit, ys-3 w40 h21 +Center vRep3_Num gPreview,
  Gui Add, UpDown, Range1-255
  Gui Add, Text, ys w15 h21,个
  Gui Add, Edit, ys-3 w80 h21 +Center vRep3_Old gPreview,
  Gui Add, Text, ys w15 h21,为
  Gui Add, Edit, ys-3 w80 h21 +Center vRep3_New gPreview,
  Gui Add, Text, section xs y%Y44% w75 h21,从文件名头第
  Gui Add, Edit, ys-3 w40 h21 +Center vIns1_Num gPreview,
  Gui Add, UpDown, Range1-255
  Gui Add, Text, ys w75 h21,个位置,插入
  Gui Add, Edit, ys-3 w120 +Center vIns1_Str gPreview,
  Gui Add, Text, section xs y%Y55% w75 h21,从文件名尾第
  Gui Add, Edit, ys-3 w40 h21 +Center vIns2_Num gPreview,
  Gui Add, UpDown, Range1-255
  Gui Add, Text, ys w75 h21,个位置,插入
  Gui Add, Edit, ys-3 w120 h21 +Center vIns2_Str gPreview,
  Gui Add, Text, section xs y%Y66% w75 h21,从文件名头第
  Gui Add, Edit, ys-3 w40 h21 +Center vDel1_Num1 gPreview,
  Gui Add, UpDown, Range1-255
  Gui Add, Text, ys w75 h21,个位置,删除
  Gui Add, Edit, ys-3 w75 h21 +Center vDel1_Num2 gPreview,
  Gui Add, UpDown, Range0-255
  Gui Add, Text, ys w45 h21,个字符
  Gui Add, Text, section xs y%Y77% w75 h21,从文件名尾第
  Gui Add, Edit, ys-3 w40 h21 +Center vDel2_Num1 gPreview,
  Gui Add, UpDown, Range1-255
  Gui Add, Text, ys w75 h21,个位置,删除
  Gui Add, Edit, ys-3 w75 h21 +Center vDel2_Num2 gPreview,
  Gui Add, UpDown, Range0-255
  Gui Add, Text, ys w45 h21,个字符
  Gui Add, Text, section x%X1%  h21,【动态预览】
  
  Gui Add, Text, section xs  h21,操作前:
  Gui Add, Text, ys  w350 h21 vOldName,原始名称
  Gui Add, Text, section xs  h21,操作后:
  Gui Add, Text, ys  w350 h21 vNewName,更改后的名称
  Gui Add, Button, section xs+60 w80 h40 ,执行
  Gui Add, Button, ys  w80 h40,预览
  Gui Add, Button, ys  w80 h40 ,清空
  Gui Add, Checkbox, ys+10 w75 h21 vContinuity,连续添加
  Gui Add, ListView,x450 y10 w500 h380,文件名|扩展名|路径
  Gui Add, Text, x450 y395  h21,Del:删除所选行;  Ctrl+Del:删除所有行
  LV_ModifyCol(1, 250)
  LV_ModifyCol(2, 50)
  LV_ModifyCol(3, 195)
  Gui, show, , %AppName%%AppVer%
  if 0>0
    gosub,GuiDropFiles
return
;}

Button执行: ;{  执行操作
  Gui,Submit,NoHide
  ErrorMsg:=""
  Loop % LV_GetCount()
  {
    LV_GetText(FileName, A_Index,1)
    LV_GetText(FileExt, A_Index,2)
    LV_GetText(FilePath, A_Index,3)
    if (RunID=1){
      NewName:=MyFunc_Run(RunID,FileName, Rep1_Old, Rep1_New)
    } else if (RunID=2){
      NewName:=MyFunc_Run(RunID,FileName, Rep2_Num, Rep2_Old, Rep2_New)
    } else if (RunID=3){
      NewName:=MyFunc_Run(RunID,FileName, Rep3_Num, Rep3_Old, Rep3_New)
    } else if (RunID=4){
      NewName:=MyFunc_Run(RunID,FileName, Ins1_Num, Ins1_Str)
    } else if (RunID=5){
      NewName:=MyFunc_Run(RunID,FileName, Ins2_Num, Ins2_Str)
    } else if (RunID=6){
      NewName:=MyFunc_Run(RunID,FileName, Del1_Num1, Del1_Num2)
    } else if (RunID=7){
      NewName:=MyFunc_Run(RunID,FileName, Del2_Num1, Del2_Num2)
    }
    OldFullPath=%FilePath%\%FileName%.%FileExt%
    NewFullPath=%FilePath%\%NewName%.%FileExt%
    IfNotExist,%OldFullPath%
    {
      ErrorMsg.=NewFullPath "`n"
    } else {
      filemove,%OldFullPath%,%NewFullPath%
      if (ErrorLevel>0 ){
        ErrorMsg.=NewFullPath "`n"
      } else {
        LV_Modify(A_Index, , NewName)
      }
    }
  }
  if (strlen(ErrorMsg)>0){
    ErrorMsg:="`n`n以下文件操作错误:`n" . ErrorMsg
  } 
  MsgBox, 64, 提示, 重命名完成!%ErrorMsg%
return
;}

Button预览: ;{  预览所有重命名
  Gui,Submit,NoHide
  PreviewStr:=""
  Loop % LV_GetCount()
  {
    LV_GetText(FileName, A_Index,1)
    if (RunID=1){
      NewName:=MyFunc_Run(RunID,FileName, Rep1_Old, Rep1_New)
    } else if (RunID=2){
      NewName:=MyFunc_Run(RunID,FileName, Rep2_Num, Rep2_Old, Rep2_New)
    } else if (RunID=3){
      NewName:=MyFunc_Run(RunID,FileName, Rep3_Num, Rep3_Old, Rep3_New)
    } else if (RunID=4){
      NewName:=MyFunc_Run(RunID,FileName, Ins1_Num, Ins1_Str)
    } else if (RunID=5){
      NewName:=MyFunc_Run(RunID,FileName, Ins2_Num, Ins2_Str)
    } else if (RunID=6){
      NewName:=MyFunc_Run(RunID,FileName, Del1_Num1, Del1_Num2)
    } else if (RunID=7){
      NewName:=MyFunc_Run(RunID,FileName, Del2_Num1, Del2_Num2)
    }
    PreviewStr.=NewName "`n"
  }
  MsgBox, 64, 重命名预览, %PreviewStr%
return
;}

Button清空: ;{  清空窗体
  Reload
return
;}

GuiDropFiles: ;{  拖动文件到窗体
  if 0 > 0
  {
    MySel=%1%
    MySel:=StrReplace(MySel,";","`n")
    ;~ msgbox % MySel
  }else {
    MySel=%A_GuiEvent%
  }
  GuiControlGet,Continuity,,Continuity
  if (Continuity=0)
    LV_Delete()
  Loop, parse, MySel, `n,`r
  {
    if (A_LoopField="")
      continue
    If InStr(FileExist(A_LoopField), "D")  ;区分是否文件夹,Attrib= D ,则是文件夹
      continue
    SplitPath,A_LoopField,MyOutFileName,MyOutDir,MyOutExt,MyOutNameNoExt,MyOutDrive
    LV_Add(,MyOutNameNoExt,MyOutExt,MyOutDir)
  }
return
;}

Preview: ;{  动态预览
  Gui,Submit,NoHide
  try
    LV_GetText(FileName, 1, 1) ; 获取首个字段的文本
  catch e
    return
  if (FileName!=""){
    GuiControl,,OldName,%FileName%
    if (RunID=1){
      NewName:=MyFunc_Run(RunID,FileName, Rep1_Old, Rep1_New)
    } else if (RunID=2){
      NewName:=MyFunc_Run(RunID,FileName, Rep2_Num, Rep2_Old, Rep2_New)
    } else if (RunID=3){
      NewName:=MyFunc_Run(RunID,FileName, Rep3_Num, Rep3_Old, Rep3_New)
    } else if (RunID=4){
      NewName:=MyFunc_Run(RunID,FileName, Ins1_Num, Ins1_Str)
    } else if (RunID=5){
      NewName:=MyFunc_Run(RunID,FileName, Ins2_Num, Ins2_Str)
    } else if (RunID=6){
      NewName:=MyFunc_Run(RunID,FileName, Del1_Num1, Del1_Num2)
    } else if (RunID=7){
      NewName:=MyFunc_Run(RunID,FileName, Del2_Num1, Del2_Num2)
    }
    GuiControl,,NewName,%NewName%
  }
return
;}

MyFunc_Run(RunID,FileName,aPara1:="",aPara2:="",aPara3:="",aPara4:=""){ ; 文件名称替换函数
  NewName:=""
  if (RunID=1){
      NewName:=StrReplace(FileName, aPara1 , aPara2)
  } else if (RunID=2){
    FoundPos := InStr(FileName, aPara2,True,1, aPara1)
    NewName:=RegExReplace(FileName,aPara2,aPara3,,1,FoundPos)
  } else if (RunID=3){
    FoundPos := InStr(FileName, aPara2,True,-1, aPara1)
    NewName:=RegExReplace(FileName,aPara2,aPara3,,1,FoundPos)
  } else if (RunID=4){
    FoundPos:=aPara1-1
    NewName:=substr(FileName,1,FoundPos) . aPara2 . substr(FileName,FoundPos+1)
  } else if (RunID=5){
    FoundPos:=StrLen(FileName)+1-aPara1
    NewName:=substr(FileName,1,FoundPos) . aPara2 . substr(FileName,FoundPos+1)
  } else if (RunID=6){
    if (aPara2=0){
      NewName:=FileName
    } else {
      FoundPos:=aPara1-1
      NewName:=substr(FileName,1,FoundPos)  . substr(FileName,FoundPos+aPara2+1)
    }
  } else if (RunID=7){
    if (aPara2=0){
      NewName:=FileName
    } else {
      FoundPos:=StrLen(FileName)+2-aPara1-aPara2
      NewName:=substr(FileName,1,FoundPos-1)  . substr(FileName,FoundPos+aPara2)
    }
  }
  return ,NewName
}

Delete:: ;{  删除选中行
  FocusedRowNumber := LV_GetNext(0, "F")  ; 查找焦点行
  LV_Delete(FocusedRowNumber)
return
;}

^Delete:: ;{  全部删除
  LV_Delete()
return
;}

GuiClose:
  ExitApp




 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。