Adjust Numbers in an expression by a set amount



  • @Leon-Bob-Noël

    I assume that this python script will do what you want to do

    from Npp import editor
    
    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)
    


  • @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



  • @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
    


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



  • @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



  • @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. :-)



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



  • 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



  • @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'):
    


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



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



  • @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.



  • @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


Log in to reply