• 欢迎访问奇跡の海网站,本站不上传任何资源,所有资源均来自于网络,欢迎加入奇跡の海~!奇跡の海-WordPress QQ群
  • 本站下载资源为网络上收集整理而来,并且以计算机技术研究交流为目的,版权归原作者所有,仅供大家参考,学习,不存在任何商业目的与商业用途.
  • 本站系统镜像均来自于官方原版,ed2k可视为P2P下载链接。所有操作系统默认均为试用版,如有正版密钥可以有效激活,本站不提供任何激活和相关服务。

AutoIt3(au3)源码-批量修改文件名小工具

AutoIT3 奇跡の海 2年前 (2017-11-03) 508次浏览 已收录 0个评论 扫描二维码

源码简介

AutoIt3(au3)源码-批量修改文件名小工具
批量修改文件名,可选择备份原始文件,以免丢失文件;提供两种模式修改,增减字符或重新命名;
进度条和文字标签可以实时显示程序执行进度;

au3源码

#cs ____________________________________

	欢迎使用 AutoIt v3 中文版 !

	Au3版本:	3.3.12.0
	脚本作者:	FlySelf
	脚本功能:	批量修改文件名
	更新日志:
	联系方式:

#ce _______________脚本开始_________________

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <GuiStatusBar.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>
#include <File.au3>
#include <Array.au3>

Opt("MustDeclareVars", 1)

Global $s_record = 0;记录所有选中的文件的总字节大小,供进度条使用
Global $a_record = "";记录已选中的文件,去除重复(数组)
Global $record_file[999]
For $i = 0 To 998
	$record_file[$i] = ""
Next

_main()
Exit


Func _main()
	Global $Form_main = GUICreate("批量修改文件名", 517, 557, -1, -1)

	Global $Group_io = GUICtrlCreateGroup("输入输出", 16, 16, 481, 97)
	Global $Label_in = GUICtrlCreateLabel("选择文件", 32, 40, 72, 17)
	Global $Input_in = GUICtrlCreateInput("", 32, 56, 166, 21)
	Global $Button_in = GUICtrlCreateButton("...", 200, 56, 26, 21)
	Global $Label_out = GUICtrlCreateLabel("保存路径", 272, 40, 72, 17)
	Global $Input_out = GUICtrlCreateInput("", 272, 56, 166, 21)
	Global $Button_out = GUICtrlCreateButton("...", 440, 56, 26, 21)
	Global $Checkbox_io = GUICtrlCreateCheckbox("备份文件(若不勾选此项,我们将删除原始文件!)", 190, 88, 290, 17)
	GUICtrlSetState(-1, $GUI_CHECKED)
	GUICtrlCreateGroup("", -99, -99, 1, 1)

	Global $Group_change = GUICtrlCreateGroup("增减字符", 16, 120, 233, 177)
	Global $Radio_add = GUICtrlCreateRadio("增加字符", 24, 144, 97, 17)
	Global $Radio_delete = GUICtrlCreateRadio("删除字符", 144, 144, 97, 17)
	Global $Checkbox_add_prefix = GUICtrlCreateCheckbox("增加前缀", 24, 176, 97, 17)
	GUICtrlSetState(-1, $GUI_DISABLE)
	Global $Checkbox_add_suffix = GUICtrlCreateCheckbox("增加后缀", 24, 200, 97, 17)
	GUICtrlSetState(-1, $GUI_DISABLE)
	Global $Checkbox_delete_prefix = GUICtrlCreateCheckbox("删除前缀", 144, 176, 97, 17)
	GUICtrlSetState(-1, $GUI_DISABLE)
	Global $Checkbox_delete_suffix = GUICtrlCreateCheckbox("删除后缀", 144, 200, 97, 17)
	GUICtrlSetState(-1, $GUI_DISABLE)
	Global $Label_prefix = GUICtrlCreateLabel("前缀字符", 36, 236, 60, 17)
	GUICtrlSetState(-1, $GUI_DISABLE)
	Global $Input_prefix = GUICtrlCreateInput("", 90, 232, 137, 21)
	GUICtrlSetState(-1, $GUI_DISABLE)
	Global $Label_suffix = GUICtrlCreateLabel("后缀字符", 36, 268, 60, 17)
	GUICtrlSetState(-1, $GUI_DISABLE)
	Global $Input_suffix = GUICtrlCreateInput("", 90, 264, 137, 21)
	GUICtrlSetState(-1, $GUI_DISABLE)
	GUICtrlCreateGroup("", -99, -99, 1, 1)

	Global $Group_rename = GUICtrlCreateGroup("重新命名", 264, 120, 233, 177)
	Global $Label_name = GUICtrlCreateLabel("主名称", 280, 156, 36, 17)
	Global $Input_name = GUICtrlCreateInput("", 323, 152, 137, 21)
	Global $Radio_pre = GUICtrlCreateRadio("增加前缀", 280, 184, 97, 17)
	Global $Radio_suf = GUICtrlCreateRadio("增加后缀", 384, 184, 97, 17)
	
	Global $Group = GUICtrlCreateGroup("", 264, 208, 233, 89)
	Global $Radio_num = GUICtrlCreateRadio("阿拉伯数字", 280, 232, 97, 17)
	GUICtrlSetState(-1, $GUI_DISABLE)
	Global $Radio_chinese_num = GUICtrlCreateRadio("大写数字", 280, 256, 97, 17)
	GUICtrlSetState(-1, $GUI_DISABLE)
	Global $Radio_capital_letter = GUICtrlCreateRadio("大写英文字母", 384, 232, 97, 17)
	GUICtrlSetState(-1, $GUI_DISABLE)
	Global $Radio_lowercase_letter = GUICtrlCreateRadio("小写英文字母", 384, 256, 97, 17)
	GUICtrlSetState(-1, $GUI_DISABLE)
	GUICtrlCreateGroup("", -99, -99, 1, 1)
	GUICtrlCreateGroup("", -99, -99, 1, 1)
	
	Global $Label1 = GUICtrlCreateLabel("没有选择文件:", 16, 320, 140, 17)
	Global $List1 = GUICtrlCreateList("", 16, 340, 240, 150)
	Global $Button_empty = GUICtrlCreateButton("清空", 200, 312, 51, 25)
	Global $Radio1 = GUICtrlCreateRadio("只增减字符", 288, 334, 97, 17)
	Global $Radio2 = GUICtrlCreateRadio("只重新命名", 288, 358, 97, 17)
	Global $Button_go = GUICtrlCreateButton("开始修改", 408, 342, 75, 25)
	Global $Label = GUICtrlCreateLabel("说明:" & @CRLF & "  1、本程序最多同时修改999个文件名;" & @CRLF & _
			"  2、若保存路径与原文件所在路径相同,则不会备份文件(即使勾选了备份文件);" & @CRLF & _
			"  3、当您选择大/小写英文字母时,文件总数不得超过26个;" & @CRLF & _
			"  4、请注意避免使用非法字符,尽管当您使用非法字符时程序也会提示。", 270, 385, 225, 104, $SS_SUNKEN)
	Global $Label2 = GUICtrlCreateLabel("", 245, 501, 260, 17, $SS_RIGHT);提示文字
	Global $Progress = GUICtrlCreateProgress(0, 518, 517, 17, $PBS_SMOOTH)
	Global $StatusBar1 = _GUICtrlStatusBar_Create($Form_main)
	Global $StatusBar1_PartsWidth[2] = [196, -1]
	_GUICtrlStatusBar_SetParts($StatusBar1, $StatusBar1_PartsWidth)
	_GUICtrlStatusBar_SetText($StatusBar1, "", 0)
	_GUICtrlStatusBar_SetText($StatusBar1, "", 1)
	_GUICtrlStatusBar_SetText($StatusBar1, @TAB & @TAB & "by FlySelf  ", 1)
	GUISetState(@SW_SHOW, $Form_main)
	
	Local $file_in
	Local $file_out
	Local $idmsg
	Local $nMsg
	While 1
		_GUICtrlStatusBar_SetText($StatusBar1, @YEAR & "年" & @MON & "月" & @MDAY & "日 " & _week() & " " & @HOUR & ":" & @MIN & ":" & @SEC, 0)
		Sleep(60)
		_idcheck();增减字符组合框内的gui优化
		
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				ExitLoop
			Case $Button_in
				$file_in = FileOpenDialog("选择文件", "::{450D8FBA-AD25-11D0-98A8-0800361B1103}", "所有文件 (*.*)", 1 + 4)
				If $file_in <> "" Then GUICtrlSetData($Input_in, $file_in)
				_record();
			Case $Button_out
				$file_out = FileSelectFolder("选择保存目录", "::{450D8FBA-AD25-11D0-98A8-0800361B1103}")
				If $file_out <> "" Then GUICtrlSetData($Input_out, $file_out)
			Case $Checkbox_io
				If GUICtrlRead($Checkbox_io) = $GUI_UNCHECKED Then
					$idmsg = MsgBox(49, "注意", "若不勾选此项,我们将删除原始文件!" & @CRLF & "确定要取消勾选吗?", -1, $Form_main)
					Switch $idmsg
						Case 1
							GUICtrlSetState($Checkbox_io, $GUI_UNCHECKED)
						Case 2
							GUICtrlSetState($Checkbox_io, $GUI_CHECKED)
					EndSwitch
				EndIf
			Case $Radio_add
				GUICtrlSetState($Checkbox_add_prefix, $GUI_ENABLE)
				GUICtrlSetState($Checkbox_add_suffix, $GUI_ENABLE)
				GUICtrlSetState($Checkbox_delete_prefix, $GUI_DISABLE)
				GUICtrlSetState($Checkbox_delete_suffix, $GUI_DISABLE)
			Case $Radio_delete
				GUICtrlSetState($Checkbox_delete_prefix, $GUI_ENABLE)
				GUICtrlSetState($Checkbox_delete_suffix, $GUI_ENABLE)
				GUICtrlSetState($Checkbox_add_prefix, $GUI_DISABLE)
				GUICtrlSetState($Checkbox_add_suffix, $GUI_DISABLE)
			Case $Radio_pre
				GUICtrlSetState($Radio_num, $GUI_ENABLE)
				GUICtrlSetState($Radio_chinese_num, $GUI_ENABLE)
				GUICtrlSetState($Radio_capital_letter, $GUI_ENABLE)
				GUICtrlSetState($Radio_lowercase_letter, $GUI_ENABLE)
			Case $Radio_suf
				GUICtrlSetState($Radio_num, $GUI_ENABLE)
				GUICtrlSetState($Radio_chinese_num, $GUI_ENABLE)
				GUICtrlSetState($Radio_capital_letter, $GUI_ENABLE)
				GUICtrlSetState($Radio_lowercase_letter, $GUI_ENABLE)
			Case $Button_empty
				GUICtrlSetData($List1, "")
				GUICtrlSetData($Label1, "没有选择文件:")
				For $i = 0 To 998
					$record_file[$i] = ""
				Next
				$a_record = ""
				$s_record = 0
			Case $Button_go
				_rename()
		EndSwitch
	WEnd
EndFunc   ;==>_main

;记录已选择的文件
Func _record()
	Local $a_list
	If StringInStr(GUICtrlRead($Input_in), "|") = 0 Then
		$a_list = StringSplit(GUICtrlRead($Input_in), "\")
		GUICtrlSetData($List1, $a_list[$a_list[0]] & "|")
		For $i = 0 To 998
			If $record_file[$i] = "" Then
				$record_file[$i] = GUICtrlRead($Input_in)
				ExitLoop
			EndIf
		Next
		;_ArrayDisplay($a_list)
		;_ArrayDisplay($record_file)
	Else
		$a_list = StringSplit(GUICtrlRead($Input_in), "|")
		If $a_list[0] > 1001 Then
			MsgBox(16, "错误", "您选择的文件总数超过了程序的限制!", -1, $Form_main)
			Return 0
		EndIf
		For $i = 2 To $a_list[0]
			GUICtrlSetData($List1, $a_list[$i] & "|")
			For $j = 0 To 998
				If $record_file[$j] = "" Then
					$record_file[$j] = $a_list[1] & "\" & $a_list[$i]
					ExitLoop
				EndIf
			Next
		Next
		;_ArrayDisplay($a_list)
		;_ArrayDisplay($record_file)
	EndIf
	$a_record = _ArrayUnique($record_file)
	GUICtrlSetData($Label1, "已选择" & ($a_record[0] - 1) & "个文件:")
	For $i = 1 To $a_record[0] - 1
		$s_record = $s_record + FileGetSize($a_record[$i])
	Next
	;_ArrayDisplay($a_record)
EndFunc   ;==>_record

;gui优化,具体见代码
Func _idcheck()
	Select
		Case GUICtrlRead($Radio_add) = $GUI_CHECKED
			If GUICtrlRead($Checkbox_add_prefix) = $GUI_CHECKED Then
				If GUICtrlGetState($Label_prefix) = 144 Or GUICtrlGetState($Input_prefix) = 144 Then;144=$GUI_DISABLE
					GUICtrlSetState($Label_prefix, $GUI_ENABLE)
					GUICtrlSetState($Input_prefix, $GUI_ENABLE)
				EndIf
			Else
				If GUICtrlGetState($Label_prefix) = 80 Or GUICtrlGetState($Input_prefix) = 80 Then;80=$GUI_ENABLE
					GUICtrlSetState($Label_prefix, $GUI_DISABLE)
					GUICtrlSetState($Input_prefix, $GUI_DISABLE)
				EndIf
			EndIf
			If GUICtrlRead($Checkbox_add_suffix) = $GUI_CHECKED Then
				If GUICtrlGetState($Label_suffix) = 144 Or GUICtrlGetState($Input_suffix) = 144 Then
					GUICtrlSetState($Label_suffix, $GUI_ENABLE)
					GUICtrlSetState($Input_suffix, $GUI_ENABLE)
				EndIf
			Else
				If GUICtrlGetState($Label_suffix) = 80 Or GUICtrlGetState($Input_suffix) = 80 Then
					GUICtrlSetState($Label_suffix, $GUI_DISABLE)
					GUICtrlSetState($Input_suffix, $GUI_DISABLE)
				EndIf
			EndIf
		Case GUICtrlRead($Radio_delete) = $GUI_CHECKED
			If GUICtrlRead($Checkbox_delete_prefix) = $GUI_CHECKED Then
				If GUICtrlGetState($Label_prefix) = 144 Or GUICtrlGetState($Input_prefix) = 144 Then
					GUICtrlSetState($Label_prefix, $GUI_ENABLE)
					GUICtrlSetState($Input_prefix, $GUI_ENABLE)
				EndIf
			Else
				If GUICtrlGetState($Label_prefix) = 80 Or GUICtrlGetState($Input_prefix) = 80 Then
					GUICtrlSetState($Label_prefix, $GUI_DISABLE)
					GUICtrlSetState($Input_prefix, $GUI_DISABLE)
				EndIf
			EndIf
			If GUICtrlRead($Checkbox_delete_suffix) = $GUI_CHECKED Then
				If GUICtrlGetState($Label_suffix) = 144 Or GUICtrlGetState($Input_suffix) = 144 Then
					GUICtrlSetState($Label_suffix, $GUI_ENABLE)
					GUICtrlSetState($Input_suffix, $GUI_ENABLE)
				EndIf
			Else
				If GUICtrlGetState($Label_suffix) = 80 Or GUICtrlGetState($Input_suffix) = 80 Then
					GUICtrlSetState($Label_suffix, $GUI_DISABLE)
					GUICtrlSetState($Input_suffix, $GUI_DISABLE)
				EndIf
			EndIf
	EndSelect
EndFunc   ;==>_idcheck

;根据条件修改文件名
Func _rename()
	Local $pre_name = GUICtrlRead($Input_prefix)
	Local $suf_name = GUICtrlRead($Input_suffix)
	Local $sDrive = "", $sDir = "", $sFilename = "", $sExtension = ""
	Local $aPathSplit = _PathSplit($a_record[1], $sDrive, $sDir, $sFilename, $sExtension)
	
	If $a_record = "" Then
		MsgBox(48, "注意", "您尚未选择任何文件!", -1, $Form_main)
	ElseIf GUICtrlRead($Input_out) = "" Then
		MsgBox(48, "注意", "您尚未选择保存路径!", -1, $Form_main)
	ElseIf GUICtrlRead($Radio1) = $GUI_UNCHECKED And GUICtrlRead($Radio2) = $GUI_UNCHECKED Then
		MsgBox(48, "注意", "请选择修改方式!", -1, $Form_main)
	ElseIf GUICtrlRead($Radio1) = $GUI_CHECKED Then
		If GUICtrlRead($Radio_add) = $GUI_UNCHECKED And GUICtrlRead($Radio_delete) = $GUI_UNCHECKED Then
			MsgBox(48, "注意", "您还没有勾选任何项!", -1, $Form_main)
		ElseIf GUICtrlRead($Radio_add) = $GUI_CHECKED Then;增加前\后缀
			If GUICtrlRead($Checkbox_add_prefix) = $GUI_CHECKED And GUICtrlRead($Checkbox_add_suffix) = $GUI_UNCHECKED Then
				If $pre_name = "" Then
					MsgBox(48, "注意", "您还没有输入前/后缀!", -1, $Form_main)
				Else
					_core(1, $pre_name, "")
				EndIf
			ElseIf GUICtrlRead($Checkbox_add_prefix) = $GUI_UNCHECKED And GUICtrlRead($Checkbox_add_suffix) = $GUI_CHECKED Then
				If $suf_name = "" Then
					MsgBox(48, "注意", "您还没有输入前/后缀!", -1, $Form_main)
				Else
					_core(1, "", $suf_name)
				EndIf
			ElseIf GUICtrlRead($Checkbox_add_prefix) = $GUI_CHECKED And GUICtrlRead($Checkbox_add_suffix) = $GUI_CHECKED Then
				If $pre_name = "" Or $suf_name = "" Then
					MsgBox(48, "注意", "您还没有输入前/后缀!", -1, $Form_main)
				Else
					_core(1, $pre_name, $suf_name)
				EndIf
			Else
				MsgBox(48, "注意", "您还没有勾选任何项!", -1, $Form_main)
			EndIf
		ElseIf GUICtrlRead($Radio_delete) = $GUI_CHECKED Then;删除前\后缀
			If GUICtrlRead($Checkbox_delete_prefix) = $GUI_CHECKED And GUICtrlRead($Checkbox_delete_suffix) = $GUI_UNCHECKED Then
				If $pre_name = "" Then
					MsgBox(48, "注意", "您还没有输入前/后缀!", -1, $Form_main)
				ElseIf StringInStr($a_record[1], $aPathSplit, 1) = 0 Then
					MsgBox(48, "注意", "您输入的字符在原文件名中不存在!", -1, $Form_main)
				Else
					_core(2, $pre_name, "")
				EndIf
			ElseIf GUICtrlRead($Checkbox_delete_prefix) = $GUI_UNCHECKED And GUICtrlRead($Checkbox_delete_suffix) = $GUI_CHECKED Then
				If $suf_name = "" Then
					MsgBox(48, "注意", "您还没有输入前/后缀!", -1, $Form_main)
				ElseIf StringInStr($a_record[1], $aPathSplit, 1) = 0 Then
					MsgBox(48, "注意", "您输入的字符在原文件名中不存在!", -1, $Form_main)
				Else
					_core(2, "", $suf_name)
				EndIf
			ElseIf GUICtrlRead($Checkbox_delete_prefix) = $GUI_CHECKED And GUICtrlRead($Checkbox_delete_suffix) = $GUI_CHECKED Then
				If $pre_name = "" Or $suf_name = "" Then
					MsgBox(48, "注意", "您还没有输入前/后缀!", -1, $Form_main)
				ElseIf StringInStr($a_record[1], $aPathSplit, 1) = 0 Then
					MsgBox(48, "注意", "您输入的字符在原文件名中不存在!", -1, $Form_main)
				Else
					_core(2, $pre_name, $suf_name)
				EndIf
			Else
				MsgBox(48, "注意", "您还没有勾选任何项!", -1, $Form_main)
			EndIf
		EndIf
	ElseIf GUICtrlRead($Radio2) = $GUI_CHECKED Then
		If GUICtrlRead($Radio_pre) = $GUI_UNCHECKED And GUICtrlRead($Radio_suf) = $GUI_UNCHECKED Then
			MsgBox(48, "注意", "您还没有勾选任何项!", -1, $Form_main)
		EndIf
		Select
			Case GUICtrlRead($Radio_pre) = $GUI_CHECKED
				If GUICtrlRead($Radio_num) = $GUI_CHECKED Then
					_core(3, 1, 0)
				ElseIf GUICtrlRead($Radio_chinese_num) = $GUI_CHECKED Then
					_core(4, 1, 0)
				ElseIf GUICtrlRead($Radio_capital_letter) = $GUI_CHECKED Then
					_core(5, 1, 0)
				ElseIf GUICtrlRead($Radio_lowercase_letter) = $GUI_CHECKED Then
					_core(6, 1, 0)
				Else
					MsgBox(48, "注意", "您还没有勾选任何项!", -1, $Form_main)
				EndIf
			Case GUICtrlRead($Radio_suf) = $GUI_CHECKED
				If GUICtrlRead($Radio_num) = $GUI_CHECKED Then
					_core(3, 0, 1)
				ElseIf GUICtrlRead($Radio_chinese_num) = $GUI_CHECKED Then
					_core(4, 0, 1)
				ElseIf GUICtrlRead($Radio_capital_letter) = $GUI_CHECKED Then
					_core(5, 0, 1)
				ElseIf GUICtrlRead($Radio_lowercase_letter) = $GUI_CHECKED Then
					_core(6, 0, 1)
				Else
					MsgBox(48, "注意", "您还没有勾选任何项!", -1, $Form_main)
				EndIf
		EndSelect
	EndIf
EndFunc   ;==>_rename

;$number值有1、2,值为1时增加字符,值为2时删除字符;
;$number值有3、4、5、6,将重新命名;此时$pre_name,$suf_name有0、1两个值,且不能同时为0或1,分别对应未选中、选中加上前/后缀
;		值为3时,按阿拉伯数字递增排序
;		值为4时,按大写数字递增排序
;		值为5时,按大写字母递增排序
;		值为6时,按小写字母递增排序
Func _core($number, $pre_name = "", $suf_name = "")
	Local $size_file = 0
	Local $out_name = GUICtrlRead($Input_out) & "\"
	Local $primary = GUICtrlRead($Input_name)
	Local $sDrive = "", $sDir = "", $sFilename = "", $sExtension = ""
	Local $aPathSplit
	Local $a_rename[$a_record[0]][5]
	Local $h_timer = TimerInit()
	
	For $i = 1 To $a_record[0] - 1
		If FileExists($a_record[$i]) = 0 Then
			MsgBox(16, "错误", "您选择的文件不存在!", -1, $Form_main)
			Return 0
		EndIf
	Next
	
	Switch $number
		Case 1
			For $i = 1 To $a_record[0] - 1
				$aPathSplit = _PathSplit($a_record[$i], $sDrive, $sDir, $sFilename, $sExtension)
				$aPathSplit[3] = $pre_name & $aPathSplit[3] & $suf_name
				;_ArrayDisplay($aPathSplit)
				For $j = 0 To 4
					$a_rename[$i][$j] = $aPathSplit[$j]
				Next
			Next
		Case 2
			For $i = 1 To $a_record[0] - 1
				$aPathSplit = _PathSplit($a_record[$i], $sDrive, $sDir, $sFilename, $sExtension)
				If $pre_name <> "" And $suf_name = "" Then
					$aPathSplit[3] = StringReplace($aPathSplit[3], $pre_name, "", 1, 1)
				ElseIf $pre_name = "" And $suf_name <> "" Then
					$aPathSplit[3] = StringReplace($aPathSplit[3], $suf_name, "", -1, 1)
				ElseIf $pre_name <> "" And $suf_name <> "" Then
					$aPathSplit[3] = StringReplace($aPathSplit[3], $pre_name, "", 1, 1)
					$aPathSplit[3] = StringReplace($aPathSplit[3], $suf_name, "", -1, 1)
				EndIf
				For $j = 0 To 4
					$a_rename[$i][$j] = $aPathSplit[$j]
				Next
			Next
		Case 3
			For $i = 1 To $a_record[0] - 1
				$aPathSplit = _PathSplit($a_record[$i], $sDrive, $sDir, $sFilename, $sExtension)
				If $pre_name = 1 And $suf_name = 0 Then
					$aPathSplit[3] = $i & $primary
				ElseIf $pre_name = 0 And $suf_name = 1 Then
					$aPathSplit[3] = $primary & $i
				Else
					MsgBox(16, "错误", "重新命名时发生内部错误!", -1, $Form_main)
					Return 0
				EndIf
				;_ArrayDisplay($aPathSplit)
				For $j = 0 To 4
					$a_rename[$i][$j] = $aPathSplit[$j]
				Next
			Next
		Case 4
			For $i = 1 To $a_record[0] - 1
				$aPathSplit = _PathSplit($a_record[$i], $sDrive, $sDir, $sFilename, $sExtension)
				If $pre_name = 1 And $suf_name = 0 Then
					$aPathSplit[3] = _numberToChinese($i) & $primary
				ElseIf $pre_name = 0 And $suf_name = 1 Then
					$aPathSplit[3] = $primary & _numberToChinese($i)
				Else
					MsgBox(16, "错误", "重新命名时发生内部错误!", -1, $Form_main)
					Return 0
				EndIf
				;_ArrayDisplay($aPathSplit)
				For $j = 0 To 4
					$a_rename[$i][$j] = $aPathSplit[$j]
				Next
			Next
		Case 5
			If $a_record[0] - 1 > 26 Then
				MsgBox(16, "错误", "本次最多修改26个文件!", -1, $Form_main)
				Return 0
			EndIf
			For $i = 1 To $a_record[0] - 1
				$aPathSplit = _PathSplit($a_record[$i], $sDrive, $sDir, $sFilename, $sExtension)
				If $pre_name = 1 And $suf_name = 0 Then
					$aPathSplit[3] = _ascToLetter($i, 1) & $primary
				ElseIf $pre_name = 0 And $suf_name = 1 Then
					$aPathSplit[3] = $primary & _ascToLetter($i, 1)
				Else
					MsgBox(16, "错误", "重新命名时发生内部错误!", -1, $Form_main)
					Return 0
				EndIf
				;_ArrayDisplay($aPathSplit)
				For $j = 0 To 4
					$a_rename[$i][$j] = $aPathSplit[$j]
				Next
			Next
		Case 6
			If $a_record[0] - 1 > 26 Then
				MsgBox(16, "错误", "本次最多修改26个文件!", -1, $Form_main)
				Return 0
			EndIf
			For $i = 1 To $a_record[0] - 1
				$aPathSplit = _PathSplit($a_record[$i], $sDrive, $sDir, $sFilename, $sExtension)
				If $pre_name = 1 And $suf_name = 0 Then
					$aPathSplit[3] = _ascToLetter($i, 0) & $primary
				ElseIf $pre_name = 0 And $suf_name = 1 Then
					$aPathSplit[3] = $primary & _ascToLetter($i, 0)
				Else
					MsgBox(18, "错误", "重新命名时发生内部错误!", -1, $Form_main)
					Return 0
				EndIf
				;_ArrayDisplay($aPathSplit)
				For $j = 0 To 4
					$a_rename[$i][$j] = $aPathSplit[$j]
				Next
			Next
	EndSwitch
	;_ArrayDisplay($a_rename)
	If GUICtrlRead($Checkbox_io) = $GUI_CHECKED Then
		For $i = 1 To $a_record[0] - 1
			$aPathSplit = _PathSplit($a_record[$i], $sDrive, $sDir, $sFilename, $sExtension)
			FileCopy($a_record[$i], $out_name, $FC_OVERWRITE + $FC_CREATEPATH)
			
			$size_file = $size_file + FileGetSize($out_name & $aPathSplit[3] & $aPathSplit[4])
			GUICtrlSetData($Progress, ($size_file / $s_record) * 100)
			GUICtrlSetData($Label2, "文件共" & ($a_record[0] - 1) & "个,正在修改第" & $i & "个")
			_GUICtrlStatusBar_SetText($StatusBar1, @YEAR & "年" & @MON & "月" & @MDAY & "日 " & _week() & " " & @HOUR & ":" & @MIN & ":" & @SEC, 0)
			_File_Rename($out_name & $aPathSplit[3] & $aPathSplit[4], $out_name & $a_rename[$i][3] & $a_rename[$i][4])
			Switch @error
				Case 1
					FileDelete($out_name & $aPathSplit[3] & $aPathSplit[4])
					MsgBox(16, "错误", "您选择的文件不存在!", -1, $Form_main)
					Return 1
				Case 2
					FileDelete($out_name & $aPathSplit[3] & $aPathSplit[4])
					MsgBox(16, "错误", "您尚未指定新文件名!", -1, $Form_main)
					Return 2
				Case 3
					FileDelete($out_name & $aPathSplit[3] & $aPathSplit[4])
					MsgBox(16, "错误", "您指定的新文件名含有非法字符!", -1, $Form_main)
					Return 3
				Case 4
					FileDelete($out_name & $aPathSplit[3] & $aPathSplit[4])
					MsgBox(16, "错误", "保存路径下已存在同名文件!", -1, $Form_main)
					Return 4
				Case 5
					FileDelete($out_name & $aPathSplit[3] & $aPathSplit[4])
					MsgBox(16, "错误", "未知错误!(如不能更改的系统目录、正在使用的目录等)", -1, $Form_main)
					Return 5
			EndSwitch
			
		Next
	Else
		For $i = 1 To $a_record[0] - 1
			$aPathSplit = _PathSplit($a_record[$i], $sDrive, $sDir, $sFilename, $sExtension)
			FileMove($a_record[$i], $out_name, $FC_OVERWRITE + $FC_CREATEPATH)
			
			$size_file = $size_file + FileGetSize($out_name & $aPathSplit[3] & $aPathSplit[4])
			GUICtrlSetData($Progress, ($size_file / $s_record) * 100)
			GUICtrlSetData($Label2, "文件共" & ($a_record[0] - 1) & "个,正在修改第" & $i & "个")
			_GUICtrlStatusBar_SetText($StatusBar1, @YEAR & "年" & @MON & "月" & @MDAY & "日 " & _week() & " " & @HOUR & ":" & @MIN & ":" & @SEC, 0)
			_File_Rename($out_name & $aPathSplit[3] & $aPathSplit[4], $out_name & $a_rename[$i][3] & $a_rename[$i][4])
			Switch @error
				Case 1
					FileDelete($out_name & $aPathSplit[3] & $aPathSplit[4])
					MsgBox(16, "错误", "您选择的文件不存在!", -1, $Form_main)
					Return 1
				Case 2
					FileDelete($out_name & $aPathSplit[3] & $aPathSplit[4])
					MsgBox(16, "错误", "您尚未指定新文件名!", -1, $Form_main)
					Return 2
				Case 3
					FileDelete($out_name & $aPathSplit[3] & $aPathSplit[4])
					MsgBox(16, "错误", "您指定的新文件名含有非法字符!", -1, $Form_main)
					Return 3
				Case 4
					FileDelete($out_name & $aPathSplit[3] & $aPathSplit[4])
					MsgBox(16, "错误", "保存路径下已存在同名文件!", -1, $Form_main)
					Return 4
				Case 5
					FileDelete($out_name & $aPathSplit[3] & $aPathSplit[4])
					MsgBox(16, "错误", "未知错误!(如不能更改的系统目录、正在使用的目录等)", -1, $Form_main)
					Return 5
			EndSwitch
		Next
	EndIf
	GUICtrlSetData($Progress, 100)
	GUICtrlSetData($Label2, "成功修改" & ($a_record[0] - 1) & "个文件,耗时" & Round(TimerDiff($h_timer) / 1000) & "秒!")
	MsgBox(64, "提示", "文件名批量修改成功!", -1, $Form_main)
	GUICtrlSetData($Progress, 0)
EndFunc   ;==>_core

;阿拉伯数字转大写数字
Func _numberToChinese($num)
	Local $chinese
	Local $t_gewei, $t_shiwei, $t_baiwei;英语水平有限,只好用拼音了。
	Local $a_num[11] = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
	Select
		Case $num >= 0 And $num <= 10 For $i = 0 To 10 If $num = $i Then $chinese = $a_num[$i] ExitLoop EndIf Next Case $num >= 11 And $num <= 99
			For $i = 11 To 99
				If $num = $i Then
					$t_shiwei = Floor($num / 10)
					$t_gewei = Mod($num, 10)
					If $t_gewei <> 0 Then
						$chinese = $a_num[$t_shiwei] & $a_num[10] & $a_num[$t_gewei]
					Else
						$chinese = $a_num[$t_shiwei] & $a_num[10]
					EndIf
					ExitLoop
				EndIf
			Next
		Case $num >= 100 And $num <= 999
			For $i = 100 To 999
				If $num = $i Then
					$t_baiwei = Floor($num / 100)
					$t_shiwei = Floor(Mod($num, 100) / 10)
					$t_gewei = Mod(Mod($num, 100), 10)
					If $t_gewei = 0 And $t_shiwei = 0 Then
						$chinese = $a_num[$t_baiwei] & "百"
					ElseIf $t_gewei = 0 And $t_shiwei <> 0 Then
						$chinese = $a_num[$t_baiwei] & "百" & $a_num[$t_shiwei] & $a_num[10]
					ElseIf $t_gewei <> 0 And $t_shiwei = 0 Then
						$chinese = $a_num[$t_baiwei] & "百" & $a_num[0] & $a_num[$t_gewei]
					ElseIf $t_gewei <> 0 And $t_shiwei <> 0 Then
						$chinese = $a_num[$t_baiwei] & "百" & $a_num[$t_shiwei] & $a_num[10] & $a_num[$t_gewei]
					EndIf
					ExitLoop
				EndIf
			Next
	EndSelect
	Return $chinese
EndFunc   ;==>_numberToChinese

;$num是值为1~26的数字,分别对应26个字母;$caseletter只有1、0两个值,为0时返回小写字母,为1时返回大写字母
Func _ascToLetter($num, $caseletter)
	Local $temp
	Switch $caseletter
		Case 0
			$temp = $num + 96
			For $i = 97 To 122
				If $temp = $i Then
					Return Chr($temp)
					ExitLoop
				EndIf
			Next
		Case 1
			$temp = $num + 64
			For $i = 65 To 90
				If $temp = $i Then
					Return Chr($temp)
					ExitLoop
				EndIf
			Next
	EndSwitch
EndFunc   ;==>_ascToLetter

;返回值:= 1 成功
;        = 0 失败,并返回 @Error 为以下值:
;                         @Error = 1--原始文件不存在
;                         @Error = 2--未指定新文件名
;                         @Error = 3--新文件名含有非法字符
;                         @Error = 4--新文件名已存在同名文件
;                         @Error = 5--未知错误(如不能更改的系统目录、正在使用的目录等)
Func _File_Rename($File_Old_Name, $File_New_Name)
	If Not FileExists($File_Old_Name) Then Return SetError(1, 0, 0)
	If $File_New_Name = '' Then Return SetError(2, 0, 0)
	If StringRight($File_Old_Name, 1) = '\' Then $File_Old_Name = StringTrimRight($File_Old_Name, 1)
	If StringInStr($File_New_Name, '\') Then $File_New_Name = StringRegExpReplace($File_New_Name, '.+\\', '')
	If StringRegExp($File_New_Name, '(\/|\:|\*|\?|\"|\<|\>|\|)') Then Return SetError(3, 0, 0)
	Local $fPath = ''
	If StringRegExp($File_Old_Name, '\\') Then $fPath = StringRegExpReplace($File_Old_Name, '(.+\\).+', '\1')
	If FileExists($fPath & $File_New_Name) Then Return SetError(4, 0, 0)
	RunWait(@ComSpec & ' /c Rename "' & $File_Old_Name & '" ' & $File_New_Name, @ScriptDir, @SW_HIDE)
	If FileExists($fPath & $File_New_Name) Then Return 1
	If Not FileExists($fPath & $File_New_Name) Then Return SetError(5, 0, 0)
EndFunc   ;==>_File_Rename

;将@WDAY的数据改为“星期*”显示
Func _week()
	Local $a_week[7] = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
	Local $week
	Switch @WDAY
		Case 1
			$week = $a_week[0]
		Case 2
			$week = $a_week[1]
		Case 3
			$week = $a_week[2]
		Case 4
			$week = $a_week[3]
		Case 5
			$week = $a_week[4]
		Case 6
			$week = $a_week[5]
		Case 7
			$week = $a_week[6]
	EndSwitch
	Return $week
EndFunc   ;==>_week


版权声明:本站所有文章和资源使用CC BY-NC-SA 4.0协议授权发布 , 转载应当以相同方式注明文章来自“SeaOMC.COM->AutoIt3(au3)源码-批量修改文件名小工具!在下边可以分享本文哦!
喜欢 (0)
[]
分享 (0)
奇跡の海
关于作者:
一个WordPress菜鸟!
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址