使用typora的粘贴图片后, 图片默认以时间的格式命名, 不利于后面查找, 当然, 如果没有查找图片的需求, 下面的内容就必要看了
我喜欢把图片保存在文件名.assets的目录下, 所以下面的代码, 都是基于此写的, 图片保存到其它目录的, 请自行修改newfileFullPath
代码比较简单, 就是读md文件, 判断标题格式和图片格式, 再重写md文件, 图片重命名



doTyporaImgChangeName(path){
if (!FileExist(path)) {
MsgBox, “md文件不存在”
return
}
SplitPath, path , OutFileName, OutDir, OutExtension, OutNameNoExt
; Msgbox % “路径: ” . path . “-文件名: ” . OutFileName . “-目录: ” . OutDir
if (OutExtension != “md”) {
return
}
OutDir.=”\”
title := “”
titleIdx := 1
newMdFile := path . “bak”
newFileList := []
oldFileList := []
FileEncoding, UTF-8-RAW
; Log(“new md” . newMdFile)
; Log(“开始读取” . path)
loop, Read, % path, % newMdFile
{
newline := A_LoopReadLine
;找到标题
if(InStr(newline, “# “)){
Array := StrSplit(newline , “# “)
title := Array[2]
; Log(Array[2])
}
;找到图片  
if(InStr(newline, “![“)){
;获取图片路径
RegExMatch(newline, “\((.*)\)”, fullName)
; fullName1=test.assets/1676632803041.png imgName.png
split:= StrSplit(fullName1, “/”)
oldFileName := split.Length() > 1 ? split[2] : split[1]
;指定重命名的文件
if (!RegExMatch(oldFileName, “^(\d+|image-\w*)\.\w+$”)) {
FileAppend, %newline%`n
continue
}
oldFileNameSplit := StrSplit(oldFileName, “.”)
newImgPartPath := title . titleIdx . “.” . oldFileNameSplit[2]
if(split.Length() > 1) {
newImgPartPath:= split[1] . “\” . newImgPartPath
}
;重命名文件
oldfileFullPath:= OutDir . fullName1
newfileFullPath:= OutDir . newImgPartPath
oldFileList.Push(oldfileFullPath)
newFileList.Push(newfileFullPath)
; Log(oldfile . ” 移动到 “)
; Log(newfile)
if !FileExist(oldfileFullPath) {
MsgBox, % “图片文件不存在, OutDir: ” . OutDir . “-fullName1: ” . fullName1
return
}
newline:= “”
titleIdx += 1
}
FileAppend, %newline%`n
}
if(newFileList.Length() > 1){
;图片重命名
For index, oldfileFullPath in oldFileList
FileMove, % oldfileFullPath, % newFileList[index]
;删除老文件
FileDelete, % path
;新文件重命名
FileMove, % newMdFile, % path, 1
; MsgBox “结束了”
}
}

