Community
    • Login

    Find and Replace via PythonScript on subfolders

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 3 Posters 586 Views 2 Watching
    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.
    • SpecialCharactersEnjoyerS Offline
      SpecialCharactersEnjoyer
      last edited by

      NPP Find & Replace doesn’t support mathematical operations and recommends(?) doing it using a PythonScript plugin. Fair enough, I went and got myself the script:

      def multiply2(m):
          return 'Mass="' + str(float(m.group(1)) * 2) + '"'
      
      editor.rereplace('Mass="([0-9]+(?:\.[0-9]+)?)"', multiply2);
      

      I tested it, it works, I’m happy. Unfortunately, I need to run this script on over 1700 files in like 100 different subfolders and I’m stumped. For some reason I thought it would be as easy as opening the main folder as a Workspace but that was perhaps a bit too optimistic.

      PeterJonesP 1 Reply Last reply Reply Quote 1
      • PeterJonesP Offline
        PeterJones @SpecialCharactersEnjoyer
        last edited by

        @SpecialCharactersEnjoyer ,

        You would have to program a loop around the editor.rereplace where you have PythonScript open a file, run the editor.rereplace, save, and close, then move on to next file.

        Since you have 100 directories with a total of 1700 files, you would have to probably have a double-nested loop, with the first loop going over the directories, and the second loop going over the files in each directory.

        That looping portion is a generic Python coding question, and isn’t specific to Notepad++ or the PythonScript plugin (ie, the answer doesn’t change if you’re using python.exe instead of using Notepad++ and the PythonScript plugin), so you can search the internet for general Python-language help on how to write loops in Python that loop over all (matching) files in all (matching) directories.

        1 Reply Last reply Reply Quote 4
        • Alan KilbornA Offline
          Alan Kilborn
          last edited by

          In addition to what Peter said, inside your loop you would call notepad.open(), then do your replacement(s), then call notepad.close().

          1 Reply Last reply Reply Quote 4

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors