2017-02-17 109 views
0

任何帮助将不胜感激。autohotkey搜索并替换文本文件中的多个变量

我有一个专门的配置表,我用它来配置有大约20个不同变量的路由器。我最终使用Notepad ++和Autohotkey来进行搜索和替换。

我会列出一个简短的清单,因为它会说明问题。

在我的配置表中,我想搜索并替换其中的所有变量文件,这些变量全部列在文件中,以创建更多Autohotkey脚本来创建标签,配置更新和电子邮件。

工作单
srwnumber
新设备
旧设备
老毂
新资产
新的串行

什么,我试图找出是有一段代码将遍历列表并替换搜索和替换变量。

我不知道该怎么办阵列的和循环正常,但我的想法去做,这将是这样的 搜索这些变量的列表,并更换 所以收件箱中的变量将是一个变量,所以才肯题。然后我可以在变量被更新时循环问题。

这样我就可以随时添加和删除变量,而不必拥有庞大的硬代码列表。

::ncvorep:: 
SetKeyDelay, 75,75 
null := "" 
inputbox, variable1v, variable2v 
If %variable1v% <> %null% 
    { 
     send ^h 
     send Workorder{tab}{+tab} 
     send Workorder{space}%variable1v% 
     send !a 
     } 
     ; this blanks out the variable if the variable1v is blank. 
    else if workorderv = %null% 
    {  send ^h 
     send workorder {tab}{+tab} 
     send ^a 
     send {backspace} 
     send !a 
Exit 
} 

更多下面。

::ncvorep:: 
SetKeyDelay, 75,75 
null := "" 
inputbox, workorderv, Enter Work Order # 
If workorderv <> %null% 
    { 
     send ^h 
     send workorder{tab}{+tab} 
     send workorder{space}%workorderv% 
     send !a 
     } 

    else if workorderv = %null% 
    {  send ^h 
     send workorder {tab}{+tab} 
     send ^a 
     send {backspace} 
     send !a 
Exit 
} 

我不得不创建类似这样的东西,与Notepad ++一起使用来搜索和替换完成。

;Last update Tue, Feb 14, 2017 15 18 03:18:47 PM 
#SingleInstance force 
#Warn 
; template to setup cvo search and replace segment. 



::ncvorep:: 
SetKeyDelay, 75,75 
null := "" 
inputbox, workorderv, Enter Work Order # 
If workorderv <> %null% 
    { 
     send ^h 
     send workorder{tab}{+tab} 
     send workorder{space}%workorderv% 
     send !a 
     } 

    else if workorderv = %null% 
    {  send ^h 
     send workorder {tab}{+tab} 
     send ^a 
     send {backspace} 
     send !a 
Exit 
} 
null := "" 
inputbox, srwnumberv, Enter Service Order # 
If srwnumberv <> %null% 
    { 
     send ^h 
     send srwnumber{tab}{+tab} 
     send srwnumber{space}%srwnumberv% 
     send !a 
     } 

    else if srwnumberv = %null% 
    {  send ^h 
     send srwnumber {tab}{+tab} 
     send ^a 
     send {backspace} 
     send !a 
Exit 
} 
null := "" 
inputbox, new-devicev, Enter New Device Name # 
If new-devicev <> %null% 
    { 
     send ^h 
     send new-device{tab}{+tab} 
     send new-device{space}%new-devicev% 
     send !a 
     } 

    else if new-devicev = %null% 
    {  send ^h 
     send new-device {tab}{+tab} 
     send ^a 
     send {backspace} 
     send !a 
Exit 
} 

null := "" 
inputbox, old-devicev, Enter Old Device Name # 
If old-devicev <> %null% 
    { 
     send ^h 
     send old-device{tab}{+tab} 
     send old-device{space}%old-devicev% 
     send !a 
     } 

    else if old-devicev = %null% 
    {  send ^h 
     send old-device {tab}{+tab} 
     send ^a 
     send {backspace} 
     send !a 
Exit 
} 
null := "" 
inputbox, old-hubv, Enter Old Connecting Device # 
If old-hubv <> %null% 
    { 
     send ^h 
     send old-hub{tab}{+tab} 
     send old-hub{space}%old-hubv% 
     send !a 
     } 

    else if old-hubv = %null% 
    {  send ^h 
     send old-hub {tab}{+tab} 
     send ^a 
     send {backspace} 
     send !a 
Exit 
} 
null := "" 
inputbox, new-assetv, Enter New Asset # 
If new-assetv <> %null% 
    { 
     send ^h 
     send new-asset{tab}{+tab} 
     send new-asset{space}%new-assetv% 
     send !a 
     } 

    else if new-assetv = %null% 
    {  send ^h 
     send new-asset {tab}{+tab} 
     send ^a 
     send {backspace} 
     send !a 
Exit 
} 
null := "" 
inputbox, new-serialv, Enter New Serial Number # 
If new-serialv <> %null% 
    { 
     send ^h 
     send new-serial{tab}{+tab} 
     send new-serial{space}%new-serialv% 
     send !a 
     } 

    else if new-serialv = %null% 
    {  send ^h 
     send new-serial {tab}{+tab} 
     send ^a 
     send {backspace} 
     send !a 
Exit 
} 
return 

回答

0

假设我理解了这个问题,下面的解决方案可能会更好。基本上,它会复制到剪贴板中,无论文本位于最前面的窗口中,遍历字典并用它们的值替换匹配键,然后粘贴修改后的内容。

警告:如果文件过大,复制到剪贴板,可能会遇到在旧系统上的一些性能问题。

F11:: 
{ 
Send, ^a 
Send, ^c 
replace := {"workorder":"not_workorder","srwnumber":"not_srwnumber","new-device":"not_new-device","old-device":"etc"} 
For start, end in replace { 
    StringReplace, clipboard, clipboard, %start%, %end%, All 
} 
Send, ^v 
} 
相关问题