Community
    • Login

    Adjust Numbers in an expression by a set amount

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    16 Posts 3 Posters 1.2k 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.
    • Leon Bob NoëlL
      Leon Bob Noël
      last edited by

      @Ekopalypse said in Adjust Numbers in an expression by a set amount:

      def increase(m):
      current_value = int(m.group(0))
      current_value = current_value+5 if current_value < 96 else 100
      return current_value

      editor.rereplace(‘(?<=line:)\d+(?=%)’, increase)

      Awesome, it shifts the lines perfectly. But is there a way to make it work on more than 1 file at a time? I have folders of from 12 to 300 files that I need to do the same thing on. It’s a great tool to have on hand that I can adjust to whatever I need, but having to be doing it 60 times is still a problem. If it’s not possible it’s not possible, but I figured there has to be a way

      EkopalypseE 1 Reply Last reply Reply Quote 0
      • EkopalypseE
        Ekopalypse @Leon Bob Noël
        last edited by Ekopalypse

        @Leon-Bob-Noël

        One solution could be to use something like this

        import os
        from Npp import editor
        
        
        def increase(m):
            # As long as only one value in a line is changed
            # one can set the default bookmark symbol to see where the changes took place
            
            # start, end = m.span(0)
            # start_line = editor.lineFromPosition(start)
            # end_line = editor.lineFromPosition(end)
            # for i in range(start_line, end_line+1):
                # editor.markerAdd(i, 24)
        
            current_value = int(m.group(0))
            current_value = current_value+5 if current_value < 96 else 100
            return  current_value
        
        
        for currrent_directory, _, files in os.walk(r'PATH_WITHOUT_TRAILING_BAKSLASH'):
            for _file in files:
                if _file.endswith('.txt'):
                    notepad.open(os.path.join(currrent_directory, _file))
                    editor.rereplace('(?<=line:)\d+(?=%)', increase)
                    # notepad.save() # automatically save changes
                    # notepad.close() # close active buffer
        
        1 Reply Last reply Reply Quote 3
        • EkopalypseE
          Ekopalypse
          last edited by

          It walks down from a given directory and loads the file with the txt extension and changes its contents if it matches something.

          Leon Bob NoëlL 1 Reply Last reply Reply Quote 0
          • Leon Bob NoëlL
            Leon Bob Noël @Ekopalypse
            last edited by

            @Ekopalypse Would this work if the extension is VTT? It’s set to open as a text document but I don’t think that counts

            Alan KilbornA 1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @Leon Bob Noël
              last edited by

              @Leon-Bob-Noël said in Adjust Numbers in an expression by a set amount:

              Would this work if the extension is VTT?

              Not as it is. But, really, is it that hard to see it?
              I mean, the relevant line in the code reads almost like English:

              if _file.endswith('.txt'):

              Exercise left to the reader to figure out what to change it to. :-)

              Leon Bob NoëlL 1 Reply Last reply Reply Quote 1
              • Leon Bob NoëlL
                Leon Bob Noël @Alan Kilborn
                last edited by

                @Alan-Kilborn oh yeah, cool. So do I not run this whithin Notepad ++ then?

                1 Reply Last reply Reply Quote 0
                • Leon Bob NoëlL
                  Leon Bob Noël
                  last edited by

                  I’ve tried running it in Notepad ++ python scripts but no matter how I do it it does not work. With a file open in the directory it does nothing, with the directory open as workspace it does nothing, I didn’t try it with those setting with TXT files, assuming that changing it to “if _file.endswith(‘.vtt’)::” didn’t work because Notepad++ wasn’t recognizing them because it doesn’t recognize the extension by default & doesn’t have the language in it’s database

                  1 Reply Last reply Reply Quote 0
                  • EkopalypseE
                    Ekopalypse
                    last edited by Ekopalypse

                    @Leon-Bob-Noël
                    It must run from within Npp but there is no need to load the file,
                    this is done by the script.
                    You just have to adjust the file ending and the root path.

                    os.walk(r'PATH_WITHOUT_TRAILING_BAKSLASH'):
                    

                    example

                    os.walk(r'C:\Documents\whatever\directory\it\is'):
                    
                    Leon Bob NoëlL 1 Reply Last reply Reply Quote 1
                    • Leon Bob NoëlL
                      Leon Bob Noël @Ekopalypse
                      last edited by Leon Bob Noël

                      @Ekopalypse Ah, thanks. Will is do recursive directories or just the root specified?

                      EkopalypseE Alan KilbornA 2 Replies Last reply Reply Quote 0
                      • EkopalypseE
                        Ekopalypse @Leon Bob Noël
                        last edited by

                        @Leon-Bob-Noël - it walks the tree recursively.

                        1 Reply Last reply Reply Quote 1
                        • Alan KilbornA
                          Alan Kilborn @Leon Bob Noël
                          last edited by

                          @Leon-Bob-Noël

                          You could just experiment with it to find out these answers, instead of asking and then waiting to have a response.

                          Leon Bob NoëlL 1 Reply Last reply Reply Quote 2
                          • Leon Bob NoëlL
                            Leon Bob Noël @Alan Kilborn
                            last edited by

                            @Alan-Kilborn if I was receiving answers while I was able to actually do it sure, but then if not I’d have to then ask if there was a way to do it. That seems much less efficient. I know when I’m helping someone I’d much rather have them ask questions right away. The longer they wait the less likely I’ll be looking at the forum to know they need a response. Maybe that’s just me, but having a question & waiting to ask it because it might not be necessary is just rude to the person giving help, while asking questions that are unnecessary right away is only really harmful to the ego of the asker, I’d rather look like I don’t know something I actually don’t know, than go to do something & have to come back & ask for more information because I was afraid of looking like an idiot, & thereby actually being an idiot

                            1 Reply Last reply Reply Quote 1
                            • First post
                              Last post
                            The Community of users of the Notepad++ text editor.
                            Powered by NodeBB | Contributors