在运行中的AutoHotkey脚本中使用变量执行字符串替换的问题求助
更新说明
INI文件处理和自编辑代码的功能已取得不少进展,但仅完成了编辑功能的函数部分,GUI及落地实现尚未完成,后续将补充更多内容。
我的室友尝试在脚本运行状态下替换其中某段内容。
目标
需求是将文件中的c:\test替换为c:\new。
目前不确定是否需要重新加载脚本,我们希望对此进行测试。
我们曾尝试将dir替换为%dir%、newdir替换为%newdir%,但出现了错误。
现在默认值已正常显示(感谢Dieisson Silva dos Santos提供的方案)。
理想方案是使用GUI实现,也欢迎其他可行方案及建议!
可能存在的问题
- tf_replace函数的调用是否存在错误?
- InputBox获取的变量是否需要添加特定前缀?
- 其他未知问题?
问题代码
;; Use a GUI to change the file. #Include %A_MyDocuments%\AutoHotKey\Lib\tf.ahk ; the a_mydocuments var needs to be written out because on many devices AHKLv1 thinks the lib directory is in C:\Program Files\Autohotkey. #Include %A_ScriptDir% #Include %A_ScriptFullPath% ; Necessary on some devices. #SingleInstance Force ;; Use a GUI to change the file. ^u:: olddir = c:\test ; Necessary for default to work in InputBox function. newdir = c:\new ; "". Gosub, updatefile ; Find all instances of %olddir% in the file and replace them with %newdir%. InputBox, oldDir, Edit AHK File, Insert OLD directory name., , 300, 140, , , , ,% olddir If ErrorLevel Return Gosub, updatefile ; change var InputBox, newDir, Edit AHK File, Insert NEW directory name., , 300, 140, , , , ,% newdir If ErrorLevel Return Return ;; Change the file with no gui. ^d:: ; The goal of ctrl+t is to replace all instances of olddir with newdir ;olddir := "c:\test" ;newdir := "c:\new" updatefile: msgbox, % A_ScriptFullPath tf_replace(olddir, newdir, A_ScriptFullPath) Return !f11::exitapp
该tf_replace函数来自tf.ahk,用于替换文件中所有指定字符串。
TF.ahk(Hi5开发)代码片段
TF_Replace(Text, SearchText, ReplaceText="") { TF_GetData(OW, Text, FileName) IfNotInString, Text, %SearchText% Return Text ; SearchText not in TextFile so return and do nothing, we have to return Text in case of a variable otherwise it would empty the variable contents bug fix 3.3 Loop { StringReplace, Text, Text, %SearchText%, %ReplaceText%, All if (ErrorLevel = 0) ; No more replacements needed. break } Return TF_ReturnOutPut(OW, Text, FileName, 0) }
内容的提问来源于stack exchange,提问作者Wolfpack'08
相关产品推荐
相关产品推荐

