Files
tools/压缩包/au3脚本/修改名称.au3
T

73 lines
2.5 KiB
AutoIt

#include <File.au3>
#include <MsgBoxConstants.au3>
; ==============================
; 脚本功能:批量重命名 index.m3u8 为 playlist.m3u8
; 作者:AI助手
; 日期:2026年1月7日
; ==============================
; 1. 选择目标目录
Local $sSourceDir = FileSelectFolder("请选择要处理的根目录", "")
If @error Then
MsgBox($MB_ICONINFORMATION, "操作取消", "您未选择任何目录,程序将退出。")
Exit
EndIf
; 2. 检查目录是否存在
If Not FileExists($sSourceDir) Then
MsgBox($MB_ICONERROR, "错误", "所选目录不存在!")
Exit
EndIf
; 3. 初始化计数器
Local $iRenamedCount = 0
Local $iErrorCount = 0
; 4. 遍历目录及子目录
Local $aFileList = _FileListToArrayRec($sSourceDir, "index.m3u8", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
If @error Then
If @error = 1 Then
MsgBox($MB_ICONINFORMATION, "完成", "在指定目录中未找到任何 index.m3u8 文件。")
Else
MsgBox($MB_ICONERROR, "错误", "遍历文件时发生错误:" & @error)
EndIf
Exit
EndIf
; 5. 处理每个找到的文件
For $i = 1 To $aFileList[0]
Local $sOldPath = $aFileList[$i]
Local $sNewPath = StringReplace($sOldPath, "index.m3u8", "playlist.m3u8")
; 检查目标文件名是否已存在
If FileExists($sNewPath) Then
; 添加数字后缀避免冲突 (例如: playlist(1).m3u8)
Local $iSuffix = 1
Local $sTempNewPath = $sNewPath
While FileExists($sTempNewPath)
$sTempNewPath = StringReplace($sNewPath, ".m3u8", "(" & $iSuffix & ").m3u8")
$iSuffix += 1
WEnd
$sNewPath = $sTempNewPath
EndIf
; 执行重命名
If FileMove($sOldPath, $sNewPath, 1) Then ; 1 表示覆盖目标文件(如果已存在且我们处理了冲突)
$iRenamedCount += 1
ConsoleWrite("成功重命名: " & $sOldPath & " -> " & $sNewPath & @CRLF)
Else
$iErrorCount += 1
ConsoleWrite("重命名失败: " & $sOldPath & " (错误代码: " & @error & ")" & @CRLF)
EndIf
Next
; 6. 显示结果
Local $sMsg = "处理完成!" & @CRLF & @CRLF & _
"成功重命名: " & $iRenamedCount & " 个文件" & @CRLF & _
"重命名失败: " & $iErrorCount & " 个文件"
MsgBox($MB_ICONINFORMATION, "结果", $sMsg)
; 可选:打开控制台窗口查看详细日志(如果使用 SciTE 编辑器运行)
; 如果你想在独立的命令行窗口看到输出,请确保脚本是通过编译或从命令行运行的。