Page 1 of 1

FileMove 重命名不生效

Posted: 12 Mar 2023, 10:28
by shouyoubetter

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就会失败

Re: FileMove 重命名不生效  Topic is solved

Posted: 13 Mar 2023, 06:04
by shouyoubetter
我找到问题所有在
FileMove, oldfile, newfile没有加%
换成FileMove, % oldfile, % newfile 即可