Post by shouyoubetter » 12 Mar 2023, 10:28
Code: Select all
Log(ByRef str, rn=1)
{
DllCall("AllocConsole"), rn:=(rn ? "`r`n":"")
FileAppend, %str%%rn%, *
}
; 关闭控制台(手动关闭会退出AHK程序)
logoff()
{
DllCall("FreeConsole")
}
typoraImgChangeName(){
path:= "E:\笔记\spring\spring概述.md"
SplitPath, path , OutFileName, OutDir, OutExtension, OutNameNoExt
OutDir.="\" . OutNameNoExt . ".assets" . "\"
title := ""
titleIdx := 1
oldfileList := []
newfileList := []
; While, flag
Log("开始读取" . path)
loop, Read, E:\笔记\spring\spring概述.md, E:\笔记\spring\spring概述2.md
{
newline := A_LoopReadLine
;找到标题
if(InStr(newline, "# ")){
Array := StrSplit(newline , "# ")
title := Array[2]
Log(Array[2])
}
;找到图片
if(InStr(newline, "![1")){
;获取图片路径
RegExMatch(newline, "\((.*)\)", fullName)
; ![1676632803041](spring概述.assets/1676632803041.png)
split:= StrSplit(fullName1, "/")
mdFile:= split[1] . "/" . title . titleIdx . ".png"
titleIdx += 1
;重命名文件
oldfile:= OutDir . split[2]
newfile:= OutDir . title . titleIdx . ".png"
Log(oldfile . " 移动到 ")
Log(newfile)
FileMove, oldfile, newfile
Sleep, 3000
oldfileList.Push(oldfile)
newfileList.Push(newfile)
newline:= StrReplace(newline, split[2], title . titleIdx . ".png")
}
FileAppend, %newline%`n
}
MsgBox "结束了"
}
typoraImgChangeName()
上面是我的代码 从Log打印出来的数据看 单独使用FileMove可以成功, 调用typoraImgChangeName就会失败
[code]Log(ByRef str, rn=1)
{
DllCall("AllocConsole"), rn:=(rn ? "`r`n":"")
FileAppend, %str%%rn%, *
}
; 关闭控制台(手动关闭会退出AHK程序)
logoff()
{
DllCall("FreeConsole")
}
typoraImgChangeName(){
path:= "E:\笔记\spring\spring概述.md"
SplitPath, path , OutFileName, OutDir, OutExtension, OutNameNoExt
OutDir.="\" . OutNameNoExt . ".assets" . "\"
title := ""
titleIdx := 1
oldfileList := []
newfileList := []
; While, flag
Log("开始读取" . path)
loop, Read, E:\笔记\spring\spring概述.md, E:\笔记\spring\spring概述2.md
{
newline := A_LoopReadLine
;找到标题
if(InStr(newline, "# ")){
Array := StrSplit(newline , "# ")
title := Array[2]
Log(Array[2])
}
;找到图片
if(InStr(newline, "![1")){
;获取图片路径
RegExMatch(newline, "\((.*)\)", fullName)
; ![1676632803041](spring概述.assets/1676632803041.png)
split:= StrSplit(fullName1, "/")
mdFile:= split[1] . "/" . title . titleIdx . ".png"
titleIdx += 1
;重命名文件
oldfile:= OutDir . split[2]
newfile:= OutDir . title . titleIdx . ".png"
Log(oldfile . " 移动到 ")
Log(newfile)
FileMove, oldfile, newfile
Sleep, 3000
oldfileList.Push(oldfile)
newfileList.Push(newfile)
newline:= StrReplace(newline, split[2], title . titleIdx . ".png")
}
FileAppend, %newline%`n
}
MsgBox "结束了"
}
typoraImgChangeName()
[/code]
上面是我的代码 从Log打印出来的数据看 单独使用FileMove可以成功, 调用typoraImgChangeName就会失败