Add Prefix And Postfix Text To Each Line In Text File

Share your macros, knowledge, solutions, services, ...
Post Reply
Petr
Posts: 106
Joined: Tue Oct 18, 2016 1:12 pm

Add Prefix And Postfix Text To Each Line In Text File

Post by Petr » Wed Nov 23, 2016 3:53 pm

This sample adds prefix and postfix text to each line in a selected text file.

Code: Select all

<#> Start of the macro
<cmds>

<#> Add Form Items-input file
<form_item>("f1","Input file:","EDIT_FILE","*.txt","vInputFile")
<#> Add Form Items-output file
<form_item>("f1","Output file:","EDIT_FILE","%_vFolder_Temp%\output.txt","vOutputFile")
<#> Add Form Items-prefix for each line
<form_item>("f1","Prefix:","EDIT",">>>","vPrefix")
<#> Add Form Items-postfix for each line
<form_item>("f1","Postfix:","EDIT","<<<","vPostfix")
<#>  Show this form

<form_show>("f1","Form","shell32.dll",0,,0)
<#> Is not last action (Form Show) cancelled? If Yes, then exit macro...

<if_str>("_vCanceled==1")<#>
<exitmacro><#>
<endif>

<#> Open text file for loading Data From File To Variable.

<data_load>("vbTemp_FileText","vInputFile","")
<#> Open file for saving text to file.

<data_save>("","vOutputFile","")
<#> If an error not occurs

<if_str>("_vErr==NO")
   <#> How many lines is in file?

   <var_oper>(vbTemp_NumOfLines,"vbTemp_FileText",TEXT_GET_NUMOFLINES,"","", "0")
   <#> Begin Loop

   <begloop>(vbTemp_NumOfLines)

      <var_oper>(vbTemp_LineText,"vbTemp_FileText",TEXT_GET_LINE,"_vLoopCounter","", "0")
     <#> Add prefix
      <var_oper>(vbTemp_LineText,"%vbTemp_LineText%",STR_INSERT,"0","%vPrefix%", "0")
      <#> Add postfix
      <var_oper>(vbTemp_LineText,"%vPostfix%%_vKeyReturn%",STR_APPEND,"","", "0")
      <#> Save lext lin in to file
      <data_save>("vbTemp_LineText","vOutputFile","A")
   
   <endloop>

<else>
   <#> Show error message

   <msg>(-100,-100,"_vError","File Error",1)
<endif>

<#> Show dialog

<msg>(-100,-100,"Do you want to open output file now?","Message",2)
<if_str>("_vMsgButton==YES")
   <fileopen>("%vOutputFile%",0)
<endif>
<#> End of the macro

Post Reply