Community
    • Login

    Find & replace with increment across multiple files

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 2.8k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Craig HillC
      Craig Hill
      last edited by

      Hello Notepad++ Community!

      I’m looking for functionality that will allow me to find & replace a common value across multiple files (all files open in the program) and to increment each value so that each is unique. Currently I use the Find and Replace functionality and ‘Replace All in All Opened Documents’, but it only replaces with the exact value specified. Once replaced I save open all files.

      Find what = replaceMe
      Replace with = ID001

      Desired output:
      ID001
      ID002
      ID003
      etc…

      I have hundreds of files that I use for testing where I need to replace different values to make them unique and am looking for a simple way to do this. I love Notepad++, but I am not a technical (programmer) user. I need a relatively simple way to perform this task that is easily repeatable and timely.

      From what I’ve read this would not be performed using regex, possibly through python or some other add-in?

      Any input the Notepad++ community can provide would be appreciated.’

      1 Reply Last reply Reply Quote 0
      • Jim DaileyJ
        Jim Dailey
        last edited by

        Here is an AWK script that may help you. You can download GNU AWK from here: https://sourceforge.net/projects/ezwinports/files/gawk-4.1.3-w32-bin.zip/download

        # **************************************************************************
        # AWK script to replace all occurences of FindStr with RepStrNNN.
        # Output files are the same name as the input files with ".ed" appended.
        #
        # Run it like this:  gawk -f .\thisScript.awk <files to be edited>
        #
        # If this file is saved as repAll.awk in the current directory, and the
        # files to be edited are in directory c:\fileDir, then:
        #
        #    gawk -f .\repAll.awk c:\fileDir\*
        #
        # **************************************************************************
        
        BEGIN {
            # If FindStr contains any characters that have special regex meaning,
            # they must be prefaced with "\". For example: "Needed\* Here".
            FindStr   = "replaceMe"
            RepStr    = "ID"
            NumDigits = 3                   # Number of digits to append to RepStr.
                                            # Controls leading zeroes, but does not
                                            # limit the total number (i.e. if there
                                            # are 10000+ replaces, then ID10134, for
                                            # example, will be generated).
            Number    = 0                   # Initial number to append.
            BatchFile = ".\\CopyAndDel.bat" # Name of batch file used to clean up.
        }
        
        FNR == 1 {
            print("@copy " FILENAME ".ed " FILENAME " /y >nul") > BatchFile
            print("@del  " FILENAME ".ed >nul") > BatchFile
        }
        
        {
            while (match($0, FindStr)) {
                sub(FindStr, sprintf("%s%0*d", RepStr, NumDigits, Number++))
            }
            print > (FILENAME ".ed")
        }
        
        END {
            print "If the '.ed' files look good, execute " BatchFile " to copy them over"
            print "the original files and then delete them."
        }
        
        1 Reply Last reply Reply Quote 1
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors