Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Replace string across multiple files and increment value

    General Discussion
    4
    6
    4507
    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.
    • Philip Lengden
      Philip Lengden last edited by

      I’m looking for an urgent solution! We need to append a text string in 1800+ XML files and increment the value by 1. Alternatively, I could append the text string with the filename of the doc. Either solution would work.

      Example 1
      Original string is Benjamin_poster.jpg across all files
      New value would be Benjamin_poster1.jpg, Benjamin_poster2.jpg etc.

      OR

      Example 2
      Original string is Benjamin_poster.jpg across all files
      New value would be Benjamin_poster[filename1].jpg, Benjamin_poster[filename2].jpg etc.

      Any help would be great.
      Thank you!

      1 Reply Last reply Reply Quote 0
      • Philip Lengden
        Philip Lengden last edited by

        $20 to the first correct submission ; )

        Claudia Frank Scott Sumner 2 Replies Last reply Reply Quote 0
        • guy038
          guy038 last edited by

          Hello, Philip Lengden,

          Oh, many thanks for your offer, but I could had help you, for free , just for the pleasure to give you a decent solution ;-))

          Unfortunately, this kind of search / replacement can’t be easily achieved, even with the powerful regular expression engine,included in N++, because it needs some calculus, based on the + operation !

          But, don’t be sad ! This task should be easily run, in few lines of code, with a Python or Lua script, which can be performed, from inside Notepad++ ! However, up to now, I’m, personally, still unable to create such scripts !

          So, Claudia, Scott and dail, here’s the solution to improve your Christmas’s holidays ! Some more beer ? An other gift ?.. I’m joking, of course :-))

          Philip, I do hope that someone will help you, very soon !

          Best regards,

          guy038

          1 Reply Last reply Reply Quote 0
          • Claudia Frank
            Claudia Frank @Philip Lengden last edited by Claudia Frank

            @Philip-Lengden

            Few questions:
            Is there only one occurrence of the search string in each file or could it be multiple?
            If there are multiple search strings in one file should all be replaced with same value
            or should it be already incremented?
            If only each file should get the incremented value, what if a file doesn’t have the search string,
            can this incremented value be discarded or is it needed to assign it to the next file. Something like

            file1 has the search string and will be replace with new_value1
            file2 has the search string and will be replace with new_value2
            file3 has not the search string
            file4 has the search string and will be replaced with value (3 or 4)?

            Cheers
            Claudia

            1 Reply Last reply Reply Quote 0
            • Scott Sumner
              Scott Sumner @Philip Lengden last edited by

              @Philip-Lengden

              I have no doubt that with @Claudia-Frank on it, you will get more than a $20 solution. :-)

              Claudia Frank 1 Reply Last reply Reply Quote 0
              • Claudia Frank
                Claudia Frank @Scott Sumner last edited by Claudia Frank

                @Scott-Sumner

                no pressure, no pressure - I thought of a simple solution like

                import os
                search_string = 'HERE_YOUR_STRING'
                i = 0
                for root, dirs, files in os.walk('C:\\WHAT_EVER_DIR\\'):  # take care of double backslash like c:\\temp\\dir1\\
                    for file in files:
                        fname, ext = os.path.splitext(file)
                        if ext == '.xml':
                            i+=1
                            full_path = os.path.join(root, file)
                            with open(full_path) as f:
                                s = f.read()
                            s = s.replace(search_string, search_string + str(i))
                            with open(full_path, "w") as f:
                                f.write(s)
                

                but now, maybe an windows api solution?? ;-))

                Cheers
                Claudia

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Copyright © 2014 NodeBB Forums | Contributors