2017-02-17 106 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 
} 
相關問題