Search in *.txt Files for a string that's not in and add it



  • Hello all,
    I am looking for a way to do the following in Notepad++ using the “Search in files”…

    1. search in all files of type *.txt for the term: Keyword=notIndexedvalue
    2. in the files where the term is not included, add it at the end of the file and save the file.

    Is this possible at all? I have not been able to find anything despite intensive search…
    It would be great if someone could help me there.
    Many thanks in advance.

    Greetings from Germany…



  • @Odyseus said in Search in *.txt Files for a string that's not in and add it:

    Is this possible at all? I have not been able to find anything despite intensive search…

    Yes I do believe it’s possible, in fact I just tested it and it appears to work.

    What we use is the “alternation” option in regular expressions. That means we take 1 of 2 paths. We either find a file with the keyword, or the alternative is the file is checked, keyword not found and the additional text added to the end of the file.

    So the Find What is:(?s-i).*?Keyword=notIndexedvalue.*\K|(.*\K)
    Replace With:(?1Keyword=notIndexedvalue)

    With alternation, the regex tries to complete the find with the first option, which is a file with the keyword included. If it fails then it takes the second option which here it reads every character, then forgets them (\K) and then adds the keyword at the end of the file.

    I did note that in a test the keyword will be added to the end of the last line. Perhaps you wish it to be on a “new” line in which case the Replace field should be (?1\r\nKeyword=notIndexedvalue)

    See if that works for you.

    Terry



  • Hi, @odyseus, @terry-r and All,

    Ah nice shot, Terry ! You’re using this powerful technique Not_This|Not_That|Go_Away|(We_Want_This), fully described in :

    https://www.rexegg.com/regex-best-trick.html

    Best Regards,

    guy038



  • @Terry-R said in Search in *.txt Files for a string that's not in and add it:

    (?s-i).?Keyword=notIndexedvalue.\K|(.*\K)

    Hello Terry,
    thank you so much… Works very fine for me…


Log in to reply