Community
    • Login

    Notepad++ Python Script - Find files containing string at specific line

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    13 Posts 4 Posters 8.0k 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.
    • toxpalT
      toxpal @Scott Sumner
      last edited by

      @Scott-Sumner thanks for your suggestions. I will check editor.getTextlength(), but searching with regex won’t suit my needs, because I only need to find files that contain closing PHP tag at exactly next-to-last line and ignore all the files that contain such a tag in other line(s).

      Scott SumnerS 1 Reply Last reply Reply Quote 0
      • Scott SumnerS
        Scott Sumner @toxpal
        last edited by

        @toxpal

        I fail to see how a regex in the call to editor.findText() or editor.research() can’t help solve your problem.

        Okay, so we can tighten up the regex (which means making it longer, ironically) to match your description better:
        (?s)<\?php\r\n.+?\?>\r\n[^\r\n]+(?:\r\n)?\z

        …but that doesn’t help aid my understanding of why you think it can’t benefit your situation.

        1 Reply Last reply Reply Quote 0
        • toxpalT
          toxpal
          last edited by

          I’ll give it a try, and let you know how it works.

          1 Reply Last reply Reply Quote 0
          • toxpalT
            toxpal
            last edited by

            Tried using regex, but script does nothing (not sure if it’s regex or script’s fault):

            import os;
            import sys;
            filePathSrc="C:\\Temp\\some_directory"
            for root, dirs, files in os.walk(filePathSrc):
                for fn in files:
                  if fn[-4:] == '.php':
                    notepad.open(root + "\\" + fn)
                    textlength=editor.getTextlength()
                    editor.findText('SCFIND_REGEXP', 0, textlength, '(?s)<\?php\r\n.+?\?>\r\n[^\r\n]+(?:\r\n)?\z')
            
            1 Reply Last reply Reply Quote 0
            • toxpalT
              toxpal
              last edited by

              Update: pretty sure it’s scripts fault because even if I write such a simple search function, it does NOT work:

              import os;
              import sys;
              filePathSrc="C:\\Temp\\some_directory"
              for root, dirs, files in os.walk(filePathSrc):
                  for fn in files:
                    if fn[-4:] == '.php':
                      notepad.open(root + "\\" + fn)
                      textlength=editor.getTextlength()
                      editor.findText('SCFIND_WHOLEWORD', 0, textlength, '<html>')
              

              What am I missing here?

              1 Reply Last reply Reply Quote 0
              • Jim DaileyJ
                Jim Dailey
                last edited by

                @toxpal

                Perhaps “<” and “>” are not considered parts of a word, so SCIFIND_WHOLEWORD fails given your “word”?

                Maybe try SCIFIND_REGEXP with a regex having no special characters (like “html”) to find out?

                1 Reply Last reply Reply Quote 0
                • toxpalT
                  toxpal
                  last edited by toxpal

                  Tried:

                  import os;
                  import sys;
                  filePathSrc="C:\\Temp\\some_directory"
                  for root, dirs, files in os.walk(filePathSrc):
                      for fn in files:
                        if fn[-4:] == '.php':
                          notepad.open(root + "\\" + fn)
                          textlength=editor.getTextlength()
                          editor.findText('SCFIND_REGEXP', 0, textlength, 'html')
                  

                  Also does nothing…

                  Scott SumnerS 1 Reply Last reply Reply Quote 0
                  • Scott SumnerS
                    Scott Sumner @toxpal
                    last edited by

                    @toxpal

                    As my second point in my first post in this thread, I pointed out that the first argument to the editor.findText() function is NOT a string. Continuing to code it as if it is a string will get you nowhere. I think that if you look at the Pythonscript console window after running the script you will see that your script isn’t doing “nothing”, it’s giving you errors. For me it gives this error when I (intentionally) specify a string as the first argument to editor.findText():

                    f = editor.findText('SCFIND_REGEXP', 0, editor.getTextLength(), 't')
                    <class 'Boost.Python.ArgumentError'>:  Python argument types in
                    Editor.findText(Editor, str, int, int, str)
                    did not match C++ signature:
                    findText(class NppPythonScript::ScintillaWrapper {lvalue}, int flags, int start, int end, class boost::python::api::object ft)
                    

                    The complete set of proper values for the first argument is found in the help docs for Pythonscript:

                    class FINDOPTION :
                    FINDOPTION.WHOLEWORD 
                    FINDOPTION.MATCHCASE 
                    FINDOPTION.WORDSTART 
                    FINDOPTION.REGEXP 
                    FINDOPTION.POSIX
                    
                    1 Reply Last reply Reply Quote 0
                    • toxpalT
                      toxpal
                      last edited by

                      Thank you for your efforts. I’m totally new to Python, so I’m having troubles with it.

                      So I modified script:

                      import os;
                      import sys;
                      filePathSrc="C:\\Temp\\some_directory"
                      for root, dirs, files in os.walk(filePathSrc):
                          for fn in files:
                            if fn[-4:] == '.php':
                              notepad.open(root + "\\" + fn)
                              console.write(root + "\\" + fn + "\r\n")
                              end_position=editor.getTextlength()
                              editor.findText(FINDOPTION.MATCHCASE, 0, end_position, "<html>")
                      

                      Now console returns error AttributeError: 'Editor' object has no attribute 'getTextlength'

                      Changing getTextlength to getLength removes all the errors in console, and it only displays a message that PHP file was opened. However, it still doesn’t find any string

                      Scott SumnerS 1 Reply Last reply Reply Quote 0
                      • Scott SumnerS
                        Scott Sumner @toxpal
                        last edited by

                        @toxpal

                        OK, so you have to read the documentation to see how these “editor” functions work. See Plugins -> Pythonscript -> Context-Help.

                        If you do this, you will see that editor.findText() returns a value indicating where a match did (or didn’t) occur.

                        Does this make sense?

                        Same thing applies to editor.getTextLength()…except in that case you actually have to spell it correctly (hint: case matters)…

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