Community
    • Login

    Macro code to Copy/Paste from one open file to another?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    17 Posts 6 Posters 2.3k 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.
    • TSquaredT
      TSquared
      last edited by

      I have discontinuous blocks of lines to copy from one file into another file. There are thousands of blocks to copy from a single file.

      I’ve been recording test macros, then editing shortcuts.xml, to build the macro to do this. So far I successfully bookmark the beginning & end of all blocks, then use a combination of “next Bookmark” and “Begin/End Select,” then copy an individual block.

      I’m not finding a macro code combination to

      • change focus to the second file;
      • goes to bottom of the file;
      • pastes the block; and
      • returns to where I came from in File #1.
        Help!

      My apologies if this is one of those newby issues that has already been dealt with multiple times, but I’m not finding them.

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

        @TSquared

        I don’t think macros will work that way. There is no ability to remember a “first” file or a “second” file, so at that point your macro-based solution goes awry. Maybe a scripting plugin solution is better? Downside is maybe more of a learning curve…

        TSquaredT 1 Reply Last reply Reply Quote 1
        • EkopalypseE
          Ekopalypse
          last edited by

          But doesn’t this mean that the next step after marking start and end of blocks is just copy bookmarked lines??

          TSquaredT 1 Reply Last reply Reply Quote 1
          • TSquaredT
            TSquared @Ekopalypse
            last edited by

            @Ekopalypse
            Yes, I have the block copied. I’m stuck trying to get to the second file in order to paste.

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

              @TSquared said in Macro code to Copy/Paste from one open file to another?:

              I’m stuck trying to get to the second file in order to paste.

              Yea, agreed; see my earlier reply.

              1 Reply Last reply Reply Quote 1
              • TSquaredT
                TSquared @Alan Kilborn
                last edited by

                @Alan-Kilborn said in Macro code to Copy/Paste from one open file to another?:

                @TSquared

                I don’t think macros will work that way. There is no ability to remember a “first” file or a “second” file, so at that point your macro-based solution goes awry. Maybe a scripting plugin solution is better? Downside is maybe more of a learning curve…

                [sigh] I was afraid of that, for the very downside that you mention. I’ll give it a bit more time to see if anyone else might have suggestions…

                1 Reply Last reply Reply Quote 2
                • Terry RT
                  Terry R
                  last edited by

                  @TSquared said in Macro code to Copy/Paste from one open file to another?:

                  There are thousands of blocks to copy from a single file

                  A slightly different way to look at this is, given you already have the ability to mark start and ends, and to also select using start and end bookmarks, could you not then bookmark ALL the lines in that selection.

                  Than select all non-bookmarked lines and delete those leaving just the ones you want and then save that to the filename you want.

                  Of course the original file becomes sacrificial using this idea so you would need to keep the ORIGINAL elsewhere and work on a copy only.

                  Terry

                  Alan KilbornA 1 Reply Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn @Terry R
                    last edited by

                    @Terry-R said in Macro code to Copy/Paste from one open file to another?:

                    could you not then bookmark ALL the lines in that selection

                    How does one bookmark all lines in a selection? Sure, one could do it manually, but if the number of lines is large it doesn’t make sense.

                    The “mark” feature can bookmark multiple lines between two (potentially far apart) points in a file, but it would have to be used with regular expressions (not for everyone) and there would have to be a unique pattern to the ending point (starting point could be “at caret”). Again, it might be too burdensome to the OP to craft something for this ending pattern.

                    I get the feeling this (what the OP described doing) is a very manual process, but one that works well for the OP…just not where multiple files are concerned.

                    Of course, though, Terry may give the solution, although it needn’t involve bookmarks: Make a copy of the original file and then manually delete the undesired text from the copy.

                    1 Reply Last reply Reply Quote 1
                    • Terry RT
                      Terry R
                      last edited by Terry R

                      @Alan-Kilborn said in Macro code to Copy/Paste from one open file to another?:

                      How does one bookmark all required lines in a selection?

                      Sorry I hadn’t given a lot of detail as the OP was just asking for suggestions. There are possibly 2 ways I can initially see. First the OP may be able to alter their selection process somewhat so that they can bookmark all lines during their process.

                      Another method I see (which I just tested to confirm is), once the selection using begin/end select has taken place (all lines between start and end bookmarks are highlighted, use the “Mark” function again and just type in the find window a single . (dot/period) and select bookmark lines. Running that using the “on selection” will bookmark all lines in the selection.

                      The 3rd alternative is a complete rehash of the process the OP currently has to use my “sacrificial” file approach and design the regexes to remove ALL unwanted lines. whether this is more doable I don’t know as no detail on the type of data is given.

                      Terry

                      EkopalypseE TSquaredT 2 Replies Last reply Reply Quote 1
                      • EkopalypseE
                        Ekopalypse @Terry R
                        last edited by

                        @TSquared

                        sorry for confusion, I thought you already bookmarked each line of the block you want to copy
                        but it looks like you marked only start and end of the blocks.

                        1 Reply Last reply Reply Quote 0
                        • TSquaredT
                          TSquared @Terry R
                          last edited by

                          @Terry-R and others:

                          Thanks so much for the input. This helps.
                          Perhaps more info would help.

                          Source File is 3.5+ million lines, in which are 1,000’s of blocks of varying lengths, all starting & ending with text lines that include “BREAK THROUGH @ WT.”

                          (so far, anyway) I bookmark all lines with that phrase. The macro jumps from bookmark to bookmark, using “Begin/End Select” to select an entire block, using the following code:

                                 <Macro name="Test_Next_BookMark" Ctrl="no" Alt="yes" Shift="no" Key="97">
                                      <Action type="2" message="0" wParam="43006" lParam="0" sParam="" />
                                      <Action type="2" message="0" wParam="42020" lParam="0" sParam="" />
                                      <Action type="2" message="0" wParam="43006" lParam="0" sParam="" />
                                      <Action type="0" message="2300" wParam="0" lParam="0" sParam="" />
                                      <Action type="2" message="0" wParam="42020" lParam="0" sParam="" />
                                      <Action type="2" message="0" wParam="42002" lParam="0" sParam="" />
                                  </Macro>
                          
                          

                          (image below is the bottom of one selected block).

                          NPP screen crop.jpg

                          Similar to @Terry-R, I’d thought of marking all the selected lines, then deleting everything not marked. Unfortunately “Mark All” doesn’t cover the entire selection (it missed 16+ lines in the image above). There can be >100 lines in one block, so I assume there’s a character or line limit. Blocks vary in length, so I can’t specify a number of lines to mark.

                          If there’s some way to automate marking all the lines in a selection, I actually like the delete-everything-but-what-I-want technique, as it avoids involving a second file.

                          I hope the extra info helps, and thanks again for all the input.

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

                            @TSquared

                            I have great luck with your scenario when I do the following bookmarking operation, even when there is ~1000 lines (it’s what I tried anyway) in the block:

                            aba56117-c50b-443c-ac6a-4d6d467ab78a-image.png

                            If you want to try exactly that, here’s some copyable text: (?s)BREAK THROUGH @ WT.*?BREAK THROUGH @ WT

                            Note that I didn’t use END for the trailing text matching, as it isn’t strictly necessary, but it could be added and should also work.

                            The key to this technique is the new feature in N++ 7.8 which will bookmark each and every line making up the marking match. Previously, only the first line of a multiline match would receive the bookmarking, although the entire match would receive the red color.

                            After you bookmark the text this way, use the feature to copy bookmarked lines to the clipboard for pasting elsewhere.

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

                              @TSquared said in Macro code to Copy/Paste from one open file to another?:

                              Unfortunately “Mark All” doesn’t cover the entire selection (it missed 16+ lines in the image above)

                              Curious, what was the expression you used to do this marking? There must be a reasonable explanation to why it stopped at that point…

                              And why is it colored in the Style #1 color (cyan)? I would have expected a reddish color, although I freely admit all of these colors can be changed from their defaults…

                              I actually like the delete-everything-but-what-I-want technique, as it avoids involving a second file.

                              This should be doable with the technique of the previous post, with application of the “inverting” of bookmarked lines command (see Search menu, Bookmark section). Or, changing the search expression used to do the marking…probably use the END text for that one…

                              1 Reply Last reply Reply Quote 1
                              • guy038G
                                guy038
                                last edited by guy038

                                Hello, @tsquared, @ekopalypse, @alan-kilborn, @terry-r and All,

                                Have you seen this difference, between N++ versions up to v7.71 and from v7.8, while marking lines in the Find dialog ?

                                For instance, using the N++ v7.7.1 version :

                                • Open the change.log. file

                                • Open the Mark dialog ( Search > Mark... )

                                • SEARCH (?s).+

                                • Tick the Bookmark line option

                                • Tick the Purge for each search option

                                • Tick the Wrap around option

                                • Select the Regular expression search mode

                                • Click on the Mark All button

                                => All non-empty lines are highlighted in red but, only the very first line is bookmarked !

                                Now, using the N++ v7.8 or v7.8.1 versions :

                                • Do the same steps as above

                                => As in the v7.7.1 version, all non-empty lines are highlighted in red but, this time, all the lines are highlighted ! It’s a very helpful improvement ! No need, anymore, to use the \K regex syntax to match, for instance, a line with a specific criteria and its next l ine, too ;-))


                                If we apply this improvement, to @Tsquared problem, it can be solved, elegantly !

                                Let consider the input text, below containing 3 zones of 5 lines, to be pasted in an other file :

                                This is a small test line to be ignored
                                This is a small test line to be ignored
                                This is a small test line to be ignored
                                START BREAK THROUGH @ WT
                                This is a small test line 01 to be pasted
                                This is a small test line 02 to be pasted
                                This is a small test line 03 to be pasted
                                This is a small test line 04 to be pasted
                                This is a small test line 05 to be pasted
                                END BREAK THROUGH @ WT
                                This is a small test line to be ignored
                                This is a small test line to be ignored
                                This is a small test line to be ignored
                                START BREAK THROUGH @ WT
                                This is a small test line 06 to be pasted
                                This is a small test line 07 to be pasted
                                
                                This is a small test line 08 to be pasted
                                This is a small test line 09 to be pasted
                                This is a small test line 10 to be pasted
                                END BREAK THROUGH @ WT
                                This is a small test line to be ignored
                                This is a small test line to be ignored
                                This is a small test line to be ignored
                                START BREAK THROUGH @ WT
                                This is a small test line 11 to be pasted
                                This is a small test line 12 to be pasted
                                This is a small test line 13 to be pasted
                                This is a small test line 14 to be pasted
                                This is a small test line 15 to be pasted
                                END BREAK THROUGH @ WT
                                This is a small test line to be ignored
                                This is a small test line to be ignored
                                This is a small test line to be ignored
                                

                                Let’s use the following regex S/R :

                                • Open the Mark dialog ( Search > Mark... )

                                • SEARCH (?s-i)^START BREAK THROUGH @ WT.+?(?=^END BREAK THROUGH @ WT)

                                • Tick the Bookmark line option

                                • Tick the Purge for each search option

                                • Tick the Wrap around option

                                • Select the Regular expression search mode

                                • Click on the Mark All button

                                => You should get the text, below, with some bookmarked lines ( noted with a ● character, at their beginning ) :

                                 This is a small test line to be ignored
                                 This is a small test line to be ignored
                                 This is a small test line to be ignored
                                ●START BREAK THROUGH @ WT
                                ●This is a small test line 01 to be pasted
                                ●This is a small test line 02 to be pasted
                                ●This is a small test line 03 to be pasted
                                ●This is a small test line 04 to be pasted
                                ●This is a small test line 05 to be pasted
                                 END BREAK THROUGH @ WT
                                 This is a small test line to be ignored
                                 This is a small test line to be ignored
                                 This is a small test line to be ignored
                                ●START BREAK THROUGH @ WT
                                ●This is a small test line 06 to be pasted
                                ●This is a small test line 07 to be pasted
                                ●
                                ●This is a small test line 08 to be pasted
                                ●This is a small test line 09 to be pasted
                                ●This is a small test line 10 to be pasted
                                 END BREAK THROUGH @ WT
                                 This is a small test line to be ignored
                                 This is a small test line to be ignored
                                 This is a small test line to be ignored
                                ●START BREAK THROUGH @ WT
                                ●This is a small test line 11 to be pasted
                                ●This is a small test line 12 to be pasted
                                ●This is a small test line 13 to be pasted
                                ●This is a small test line 14 to be pasted
                                ●This is a small test line 15 to be pasted
                                 END BREAK THROUGH @ WT
                                 This is a small test line to be ignored
                                 This is a small test line to be ignored
                                 This is a small test line to be ignored
                                

                                Now, simply :

                                • Use the N++ command Search > Bookmark > Copy Bookmarked Lines

                                • Switch to your other file or open a new file ( Ctrl + N )

                                • Paste all the bookmarked lines , in right order, with the command Edit > Paste ( Ctrl + V )

                                And you’ll get the expected text :

                                START BREAK THROUGH @ WT
                                This is a small test line 01 to be pasted
                                This is a small test line 02 to be pasted
                                This is a small test line 03 to be pasted
                                This is a small test line 04 to be pasted
                                This is a small test line 05 to be pasted
                                START BREAK THROUGH @ WT
                                This is a small test line 06 to be pasted
                                This is a small test line 07 to be pasted
                                
                                This is a small test line 08 to be pasted
                                This is a small test line 09 to be pasted
                                This is a small test line 10 to be pasted
                                START BREAK THROUGH @ WT
                                This is a small test line 11 to be pasted
                                This is a small test line 12 to be pasted
                                This is a small test line 13 to be pasted
                                This is a small test line 14 to be pasted
                                This is a small test line 15 to be pasted
                                

                                Note the different copied zones are still separated with the line START BREAK THROUGH @ WT. If, in addition, you don’t need them, the regex S/R, below, will delete all !

                                SEARCH ^START BREAK THROUGH @ WT\R

                                REPLACE Leave EMPTY

                                Best Regards,

                                guy038

                                TSquaredT 1 Reply Last reply Reply Quote 2
                                • TSquaredT
                                  TSquared @guy038
                                  last edited by

                                  @guy038, @alan-kilborn, and All,

                                  Thanks SO MUCH for what seems parallel evolution of an elegant solution.

                                  Sorry it’s taken me so long to respond: I’ve been hard at work applying your solution. This results in another challenge: When I choose “Copy bookmarked lines,” N++ freezes. I blame it on the large file size - 3.5 million lines, including 5300+ bookmarked lines. If I break it into 3 “smaller” files and bookmark/copy from each of them (so 1500-some bookmarked lines) N++ will freeze for roughly 20 minutes while it copies. Once copied then it’s easy/quick to paste in a separate file.
                                  Any recommendations for speeding up the copying, or otherwise avoiding the slow-down?

                                  1 Reply Last reply Reply Quote 0
                                  • PeterJonesP
                                    PeterJones
                                    last edited by

                                    @TSquared said in Macro code to Copy/Paste from one open file to another?:

                                    Any recommendations for speeding up the copying, or otherwise avoiding the slow-down?

                                    Move to a full-fledged programming language, which will allow you to grab the source line-by-line, and decide based on your logic whether it should be output to the new file or not. This will prevent the need for huge copy-paste buffers, or for even having the whole original file in memory.

                                    1 Reply Last reply Reply Quote 0
                                    • guy038G
                                      guy038
                                      last edited by guy038

                                      Hi, @tsquared, @ekopalypse, @alan-kilborn, @terry-r and All,

                                      Ah, OK : You’ve got some trouble because of the huge file size :-((

                                      So, instead of trying to bookmark some lines and select bunches of lines, here is a method which just get rid of any single unwanted line !. May be, it’ll be quicker !

                                      First, some assumptions :

                                      • No pure blank lines or lines with blank characters only occur in your file ( Important )

                                      • The blocks to keep start with the line START BREAK THROUGH @ WT, with that exact case, possibly preceded with some blank characters

                                      • The blocks to keep end with the line END BREAK THROUGH @ WT, with that exact case, possibly preceded with some blank characters

                                      • You’ll have to find out a dummy character, used as a temporary symbol, for correct execution of the different regexes. To that purpose, open the Find dialog and type in any single symbol, in the Find what: zone. If a click, on the Count button, produces the message Count : 0 match, this symbol can be used :-) As for me, I chose the # symbol


                                      Let’s begin :

                                      • First, run the Edit > Line Operations > Remove Empty Lines ( Containing Blank characters ) option

                                      • Add a pure blank line on the very beginning of your file

                                      • Place the caret ( cursor ) on that empty line, beginning your file ( Important )

                                      • Perform this first regex S/R, which adds a # char at the end of the Start header line and at the end of all lines to be kept :

                                        • SEARCH (?-is)(?:^(\h*START BREAK THROUGH @ WT)(\R)|\G)((?:(?!END BREAK THROUGH @ WT).)+)(\R)

                                        • REPLACE (?1\1#\2)\3#\4

                                      • Then, perform this second regex S/R, which simply add a # symbol at the end of the End header line

                                        • SEARCH (?-i)^\h*END BREAK THROUGH @ WT

                                        • REPLACE $0#

                                      • Finally, run this third regex S/R, which should delete any line which does not end with the # symbol, as well as the # symbol, itself, on lines to be kept

                                        • SEARCH (?-s)^.+[^#\r\n]\R|#

                                        • REPLACE Leave EMPTY

                                      Hope this helps you to modify your file more easily !

                                      Best Regards,

                                      guy038

                                      P.S. :

                                      I now realize that, after execution of the third regex S/R, any line START BREAK THROUGH @ WT is preceded with a line END BREAK THROUGH @ WT, so may be this End header line is useless for correct identification of the different sections

                                      In that case, just don’t run the second regex S/R and all the End header lines will be deleted as well ;-))

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