0

I am a modder with very little scripting experience. I need a way to renumber the following Sections of a file:

[Equipment 45]
 ..
 ..
 ..
 [Equipment 46]

 to 

 [Equipment 91]
 ..
 ..
 ..
 [Equipment 92]

etc.

The file looks like this:

[Equipment 44]
ID=EqpEmblem1
NameDisplayable= Real men need no emblem
FunctionalType= EqFTypeCoating
EquipmentInterval= NULL, NULL
EquipmentSlotType=NULL
ExternalLinkName3D= NULL
Hitpoints= 10000
DamageDescription1= NULL,   0,  1,  0,  1,  1,  invulnerable,       0,  0,  NULL,   0,  1,  1

[Equipment 45]
ID=EqpEmblem2
NameDisplayable= U-1164
FunctionalType= EqFTypeCoating
EquipmentInterval= NULL, NULL
EquipmentSlotType=NULL
ExternalLinkName3D=data\Textures\TNormal\tex\U-1164.dds
Hitpoints= 10000
DamageDescription1= NULL,   0,  1,  0,  1,  1,  invulnerable,       0,  0,  NULL,   0,  1,  1

and has to be continued like this:

[Equipment 91]
ID=EqpEmblem1
NameDisplayable= Real men need no emblem
FunctionalType= EqFTypeCoating
EquipmentInterval= NULL, NULL
EquipmentSlotType=NULL
ExternalLinkName3D= NULL
Hitpoints= 10000
DamageDescription1= NULL,   0,  1,  0,  1,  1,  invulnerable,       0,  0,  NULL,   0,  1,  1

[Equipment 92]
ID=EqpEmblem2
NameDisplayable= U-1164
FunctionalType= EqFTypeCoating
EquipmentInterval= NULL, NULL
EquipmentSlotType=NULL
ExternalLinkName3D=data\Textures\TNormal\tex\U-1164.dds
Hitpoints= 10000
DamageDescription1= NULL,   0,  1,  0,  1,  1,  invulnerable,       0,  0,  NULL,   0,  1,  1

and so on.

So simply spoken all [Equipment n+1] down from [Equipment 90]

All I had to do with coding was simple Java stuff in school and some Elderscrolls scripting language.

I'd like to use regex search and replace or the automation scripts plugin in Notepad++

halfer
  • 19,824
  • 17
  • 99
  • 186
The Niwo
  • 1
  • 1
  • Welcome. To be clear, what range of numbers do you want to affect, and how do you want to affect them? There is some mixed messaging and obscurity regarding these points as your question stands. – J0e3gan Jul 30 '15 at 17:27
  • Hi, Thanx for the answer. The Range is below 300. I want to insert parts of one (modded) file into another (unmodded). The thing is [Equipment nn] needs to be continous. The Problem is that the last Equipment Group of the original file ends with [Equipment 90] but the part I need from the modded file starts with [Eqiupment 44]. I just want to continue the running numbers. (I m german, I dont know if this is the correct term). I tried it with search and replace with the term Equipment ([0-9]+) but I cant find a regexp for the replace line to increment the value. i need a scripted while loop. – The Niwo Jul 30 '15 at 18:53
  • I think i found something i can use now. [link](http://stackoverflow.com/questions/1105621/find-replace-but-increment-value) – The Niwo Jul 30 '15 at 19:01
  • OK, that worked for me and I was looking for exactly such a script. I ignored it because the download for python script plugin at sf.org was offline. So this is [SOLVED], but thanks anyway ;D – The Niwo Jul 30 '15 at 19:38
  • Great. Consider posting the answer to your question for other's benefit. It is okay to answer your own question, and not all Notepad++ users are familiar with using Python within the editor, myself included. – J0e3gan Jul 30 '15 at 19:44

2 Answers2

0

Ok i used the following script for python:

from Npp import *
import re, string

# First we'll start an undo action, then Ctrl-Z will undo the actions of the whole script
editor.beginUndoAction()

expression     = notepad.prompt("Enter the search string on the first line, followed by Ctrl+Enter, \n" +
                                "followed by the replace string on second line",
                                "Incremental Search/Replace" ,
                                "")

expressionList = re.split(r"[\n\r]+", expression)

if len(expressionList) == 2:
    foundCount = [0]

    def incrementalReplace(parmReplaceStr,parmFoundCount):
        varPatternI  = r'\\i\((?P<starting>[0-9]+)\)'
        varPatternIi = r'\\i'
        varPatternISearch = re.search(varPatternI , parmReplaceStr)

        if varPatternISearch != None:
            varFoundCount    = int(varPatternISearch.group('starting')) + parmFoundCount[0]
            varReplaceString = re.sub(varPatternI ,str(varFoundCount    ),parmReplaceStr)
        elif re.search(varPatternIi, parmReplaceStr) != None:
            varReplaceString = re.sub(varPatternIi,str(parmFoundCount[0]),parmReplaceStr)

        parmFoundCount[0] = parmFoundCount[0] + 1    
        return varReplaceString

    # Do a Python regular expression replace
    editor.searchAnchor()
    while editor.searchNext(0x00200000,expressionList[0]) != -1:
        editor.replaceSel(incrementalReplace(expressionList[1],foundCount))
        editor.lineDown() 
        editor.searchAnchor()

    # End the undo action, so Ctrl-Z will undo the above two actions
    editor.endUndoAction()

Then I just entered the following in the dialog (The cursor, of course has to be 1 line above the text that needs to be renamed, the script works down the text):

Equipment [0-9]+

Equipment \i(91)

The first line finds any string containing "Equipment" and digits The second replaces every line found with the string "Equipment" and increments the digits +1 starting from 91.

God bless RegExp

Community
  • 1
  • 1
The Niwo
  • 1
  • 1
0

Auto renumbering sections with Notepad++ automated scripts

I modified the Python script to permit

  1. \i add number from 0 with step=1 (like above)
  2. \i(x) add number from x with step=1 (like above)
  3. NEW \i(x,y) add number from x with step=y
  4. NEW number will be written with the same number of characters of x number
  5. ex1: \i(8,1) will write 8, 9, 10, 11
  6. ex2: \i(008,1) will write 008, 009, 010, 011

I comment #editor.lineDown() to avoid the script will change one time yes and one time not

All changes will be made from cursor point to the end of file

from Npp import *
import re, string

# First we'll start an undo action, then Ctrl-Z will undo the actions of the whole script
editor.beginUndoAction()
expression = notepad.prompt("1st line = Search string, followed by Ctrl+Enter, 2nd line = Replace string\n" +
                            "\\i start=0 step=1 - \\i(0005) start=5 step=1 write 0005 - \\i(3,10) start=3 step=10",
                            "Incremental Search/Replace" ,
                            "")

expressionList = re.split(r"[\n\r]+", expression)

if len(expressionList) == 2:
  FoundCount = 0
  FoundStep = 0
  varPatternI = [0, 1, 2]
  varPatternI[0] = r'\\i'
  varPatternI[1] = r'\\i\((?P<starting>[0-9]+)\)'
  varPatternI[2] = r'\\i\((?P<starting>[0-9]+),(?P<step>[0-9]+)\)'

  varPatternISearch = re.search(varPatternI[2] , expressionList[1])
  if varPatternISearch != None:
    FoundCount = int(varPatternISearch.group('starting'))
    Fill = len(varPatternISearch.group('starting'))
    FoundStep = int(varPatternISearch.group('step'))
    varReplace = 2
  elif re.search(varPatternI[1], expressionList[1]) != None:
    varPatternISearch = re.search(varPatternI[1] , expressionList[1])
    FoundCount = int(varPatternISearch.group('starting'))
    Fill = len(varPatternISearch.group('starting'))
    FoundStep = 1
    varReplace = 1
  elif re.search(varPatternI[0], expressionList[1]) != None:
    FoundCount = 0
    Fill = 1
    FoundStep = 1
    varReplace = 0

  def incrementalReplace(parmReplaceStr, parmFoundCount, parmPattern):
    varPatternSearch = re.search(parmPattern, parmReplaceStr)
    if varPatternSearch != None:
      varReplaceString = re.sub(parmPattern, parmFoundCount, parmReplaceStr)
    return varReplaceString

  # Do a Python regular expression replace
  #editor.gotoPos(0)      #de-comment if you want to start from beginning of file
  editor.searchAnchor()
  while editor.searchNext(0x00200000, expressionList[0]) != -1:        
    editor.replaceSel(incrementalReplace(expressionList[1],  str(FoundCount).zfill(Fill), varPatternI[varReplace]) )
    FoundCount = FoundCount + FoundStep
    # editor.lineDown()     #go to next line 
    editor.searchAnchor()

  # End the undo action, so Ctrl-Z will undo the above two actions
  editor.endUndoAction()