Community
    • Login

    how to run my python script using (pythonscript for npp) to multiple opened files

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 3 Posters 890 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.
    • Redeye123R
      Redeye123
      last edited by

      As of now i got this working in a single file

      def times_2(m):
          return 'unit_reward_per_km: ' + str(int(m.group(1)) * 2)
      
      editor.rereplace('unit_reward_per_km: ([0-9]+)', times_2);
      notepad.save()
      

      But i need to change 200+ files and after 10 i was allready done with doing it manual . after some searching i found this but i dont know how to implement the above to the new script (I kept getting times_2 needs exact 1 argument but none given ) . and only the last file gets changed

      from Npp import *
      
      def forEachFile(func):
      	for file in notepad.getFiles():
      		notepad.activateBufferID(file[1])
      		func()
              
      def times_2(m):
          return 'unit_reward_per_km: ' + str(int(m.group(1)) * 2)
      
      def printFilename():
      	print notepad.getCurrentFilename()
      
      editor.rereplace('unit_reward_per_km: ([0-9]+)', times_2);
      notepad.save()
      
      # store the current buffer id
      currentBufferID = notepad.getCurrentBufferID()
      # run our printFilename function on each open file
      forEachFile(printFilename)
      # restore the current buffer from the stored buffer id.
      notepad.activateBufferID(currentBufferID)
      

      Anyone able to help me out ?

      mpheathM 1 Reply Last reply Reply Quote 1
      • mpheathM
        mpheath @Redeye123
        last edited by

        @redeye123

        Looks like half done with print filename… and multiple and single file code tested.

        from Npp import *
        
        def forEachFile():
        	for file in notepad.getFiles():
        		notepad.activateBufferID(file[1])
        		notepad.getCurrentFilename()
        		editor.rereplace('unit_reward_per_km: ([0-9]+)', times_2)
        		notepad.save()
        
        def times_2(m):
        	return 'unit_reward_per_km: ' + str(int(m.group(1)) * 2)
        
        forEachFile()
        

        You had most of the code already done. Just needed are little rearrangement to insert it into the loop.

        mpheathM Redeye123R 2 Replies Last reply Reply Quote 2
        • mpheathM
          mpheath @mpheath
          last edited by

          In code I posted, notepad.getCurrentFilename() will print nothing unless preceded with the print statement or use the console object console.write(notepad.getCurrentFilename()) method. That will output some progress to the console.

          Alan KilbornA 1 Reply Last reply Reply Quote 2
          • Alan KilbornA
            Alan Kilborn @mpheath
            last edited by Alan Kilborn

            @mpheath

            I would do your for loop this way instead:

            for (filename, bufferID, index, view) in notepad.getFiles():
                    notepad.activateBufferID(bufferID)
                    print(notepad.getCurrentFilename()
                    # ...
            
            1 Reply Last reply Reply Quote 3
            • Redeye123R
              Redeye123 @mpheath
              last edited by

              @mpheath said in how to run my python script using (pythonscript for npp) to multiple opened files:

              from Npp import *

              def forEachFile():
              for file in notepad.getFiles():
              notepad.activateBufferID(file[1])
              notepad.getCurrentFilename()
              editor.rereplace(‘unit_reward_per_km: ([0-9]+)’, times_2)
              notepad.save()

              def times_2(m):
              return 'unit_reward_per_km: ’ + str(int(m.group(1)) * 2)

              forEachFile()

              Yea i copied alot to help myself out and it worked in one so i had to be close the printname was just something that was in a piece of code :P and forgot to remove it when i posted it here . Thank you !

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