Adjust Numbers in an expression by a set amount

  • Re: Replacing number with another Incrementing #

    I am trying to adjust some subtitle files, so I have about 60 files that all need the same adjustments. The problem is that it’s not all the same numbers.
    For example,

    00:00:36.250 line:77%
    00:00:36.250 line:84%
    00:00:42.333 line:85%
    00:00:47.458 line:84%

    I need to move everything down by 5% so all the 84% should be 89%, all the 85% should be 90%, all the 77% should be 82%, etc. The 84, 85, 76, & 77 are common enough that I could do a general search for all of them, but every subtitle is just slightly off of where it should be. the ones that are a problem are the random ones at 13% or 24% or 35%. I saw another post that had a somewhat similar issue that was a bit different, so I figured I would see if someone knows something that’d help.

  • @Leon-Bob-Noël

    I don’t know exactly what you are going to do now.
    Do you want to increase all percentages by 5 or not?
    What if values are greater than or equal to 96%, do you want to set them to 100%? Or is there an upper limit?

  • So I’m trying to increase the values by 5%. 100% is the upper limit, but 87% is the highest I’ve seen. The ones at the bottom 84, 85, with a couple at 86 & 87 I’m completely removing the line:84% value entirely because I want those to be at the bottom. So I’m not worried about it hitting the upper limit. Oh, yeah, so It does % by % down from the top, so when i say down that’s a higher number, sorry about that.
    I want every “line:??%” value to increase by +5% of whatever is there. if there happens to be something that goes over 100% it will be ignored by VTT anyways. I’ve done for 1 folder doing a [Find In Files] starting at 99% & going down increasing everything by 5%, but it took a lot of time & I have to repeat the process for other shows when I find the appropriate % I need to shift. I figure there has to be a simpler way to do it.
    Thanks for the help

  • @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(
        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(
    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(
        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'):
      , _file))
                editor.rereplace('(?<=line:)\d+(?=%)', increase)
                # # 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.




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