• Login
Community
  • Login

Regex: Copy text between one html file to more html files (from a folder to another)

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
13 Posts 5 Posters 660 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.
  • R
    Robin Cruise
    last edited by Robin Cruise Apr 26, 2021, 8:32 PM Apr 26, 2021, 8:29 PM

    In folder 1 I have a file1.html with this code

    <!-- PART1 -->
          <div class="Field">
              <div align="right">
                 TEXT TEXT A A A
             </div>
            </div>
    <!-- PART2 -->
    

    In folder 2 I have more html files with this kind of code:

    <!-- PART1 -->
          <p>Message from my computer:</p>
          <p><samp>File not found.<br>Press F1 to continue</samp></p>
    <!-- PART2 -->
    

    So the content between <!-- PART1 --> to <!-- PART2 --> in file1.html needs be copied in all html files in folder 2 at the same <!-- PART1 --> to <!-- PART2 --> place.

    Is it possible to make this parsing just with a REGEX formula?

    I know that is possible with PowerShell, but I would prefer something simpler and faster with regex only. Is this possible?

    # Sourcefile contains text to insert
    $sourcefile = "c:/Folder1/file1.html"
    # Get content to insert
    $sourceContent = Get-Content -Path $sourcefile  -Raw
    # Get target files
    $destinationFiles = Get-ChildItem -Path "c:/Folder2" -Filter "*.html";  
    # Do for each file in destination folder
    foreach ($file in $destinationFiles) {
        # Prepare regex
        $contentToInsert = [regex]::match($sourceContent,'(?ms)<!-- PART1 -->(.+)<!-- PART2 -->').value
        # Get content of destination file
        $destinationContent = Get-Content $file.FullName -Raw
        # Replace the text in destination file content
        $destinationContent = $destinationContent -replace '(?ms)<!-- PART1 -->(.+)<!-- PART2 -->',$contentToInsert
        # Write back destination file
        Set-Content -Path $file.FullName -Value $destinationContent -Encoding UTF8
    } #end foreach file
    
    1 Reply Last reply Reply Quote 0
    • A
      Alan Kilborn
      last edited by Apr 26, 2021, 9:25 PM

      Is it possible to make this parsing just with a REGEX formula?

      @PeterJones

      Maybe we need wording in the N++ user manual for what regular expression replacement cannot do.
      File to file, folder to folder, these are good candidates.
      I mean, well, it should be obvious because there aren’t any statements in the relevant sections about these types of things, but we get questions like these.
      At least if it were in the manual, it would be possible to brush off such questions with a RTFM.

      P 1 Reply Last reply Apr 26, 2021, 9:36 PM Reply Quote 1
      • P
        PeterJones @Alan Kilborn
        last edited by Apr 26, 2021, 9:36 PM

        @Alan-Kilborn said in Regex: Copy text between one html file to more html files (from a folder to another):

        Is it possible to make this parsing just with a REGEX formula?

        @PeterJones

        Maybe we need wording in the N++ user manual for what regular expression replacement cannot do.
        File to file, folder to folder, these are good candidates.
        I mean, well, it should be obvious because there aren’t any statements in the relevant sections about these types of things, but we get questions like these.
        At least if it were in the manual, it would be possible to brush off such questions with a RTFM.

        I don’t think a user manual should have to maintain a list of things the application cannot do. That seems a better job for the FAQ section of the forum, instead.

        1 Reply Last reply Reply Quote 2
        • G
          guy038
          last edited by guy038 Apr 26, 2021, 11:42 PM Apr 26, 2021, 11:42 PM

          Hi, @robin-cruise, @alan_kilborn, @peterjones and all,

          So, Robin, if I fully understand you, you would like to replace any zone <!-- PART1 -->•••••••••<!-- PART2 --> in all HTML files of the sub-folder folder 2 with the zone, below :

          <!-- PART1 -->
                <div class="Field">
                    <div align="right">
                        TEXT TEXT A A A
                    </div>
                </div>
          <!-- PART2 -->
          

          If I’m right about it, that’s rather easy with regexes :

          • Backup all your HTML files of folder 2 within an other folder ! ( A common sense action ! )

          • Open the Find in Files dialog ( Ctrl + Shift + F )

            • SEARCH (?s-i)<!-- PART1 -->.+?<!-- PART2 -->

            • REPLACE <!-- PART1 -->\r\n <div class="Field">\r\n <div align="right">\r\n TEXT TEXT A A A\r\n </div>\r\n </div>\r\n<!-- PART2 -->

            • FILTERS *.html

            • DIRECTORY The ABSOLUTE location of "folder 2"

            • Select the Regular expression search mode

            • Click on the Replace in Files dialog

            • Hit the OK button of the Are you sure? dialog


          So, @alan-kilborn and @peterjones, in this specific case, the goal to reach seems still possible with regexes ;-))

          Best Regards,

          guy038

          T 1 Reply Last reply Apr 26, 2021, 11:49 PM Reply Quote 1
          • T
            Terry R @guy038
            last edited by Apr 26, 2021, 11:49 PM

            @guy038 said in Regex: Copy text between one html file to more html files (from a folder to another):

            in this specific case, the goal to reach seems still possible with regexes ;-))

            Actually I think you missed the statement that file1.html (in 1 folder) contains the source “code” that needs replacing a “no code present” area contained within many files in a different folder. Hence as the others mentioned, working with 2 files simultaneously, which regex is not capable of doing.

            I had considered mentioning that appending file1.html to ALL the other files, then the “Find in Files” option (with a suitable regex) would be able to:

            1. move the code to the new location and
            2. remove the remainder of the file1.html string that was appended.
              But then I thought this is far more work than his already thought about (and supplied) powershell solution.

            Terry

            1 Reply Last reply Reply Quote 0
            • G
              guy038
              last edited by guy038 Apr 27, 2021, 12:30 AM Apr 27, 2021, 12:20 AM

              Hi, @robin-cruise, @terry-r, @alan-kilborn, @peterjones and all,

              @terry-r :

              Many thanks for your comments However, I’m sorry as I still don’t see in which way I’m wrong :-((

              @robin-cruise said :

              So the content between <!-- PART1 --> to <!-- PART2 --> in file1.html needs be copied in all html files in folder 2 at the same <!-- PART1 --> to <!-- PART2 --> place.

              In my regex S/R, the part of text to be copied from File1.html is placed in the replace zone :

              • REPLACE <!-- PART1 -->\r\n <div class="Field">\r\n <div align="right">\r\n TEXT TEXT A A A\r\n </div>\r\n </div>\r\n<!-- PART2 -->

              and will replace, as @robin-cruise said, the same <!-- PART1 -->•••••••<!-- PART2 --> area in all files of folder 2 with the search zone :

              • SEARCH (?s-i)<!-- PART1 -->.+?<!-- PART2 -->

              So, I’m missing something obvious ! Could someone enlightens me with a short file1.html text, supposed to be in folder 1 and a short file2.html text, supposed to be in folder 2 ? TIA !

              BR

              guy038

              1 Reply Last reply Reply Quote 0
              • T
                Terry R
                last edited by Apr 27, 2021, 12:24 AM

                @guy038 said in Regex: Copy text between one html file to more html files (from a folder to another):

                So, I’m missing something obvious !

                Your solution involves grabbing the “source” first, then using that in the 2nd step which is the replacement using Find in Files. The OP was asking whether regex can simultaneously grab the source data, then edit the “other” files and insert it into all those files. So his request was to “open” 2 files together and “regex” them.

                Of course your solution would work if the “source code” is short enough.

                Terry

                1 Reply Last reply Reply Quote 1
                • G
                  guy038
                  last edited by guy038 Apr 27, 2021, 9:21 AM Apr 27, 2021, 12:45 AM

                  Hi, @robin-cruise, @terry-r, @alan-kilborn, @peterjones and all,

                  Ah…yes, I see what you mean, Terry !


                  Now, let’s imagine that Notepad++ has these two new native features ( just a dream !! ) :

                  • A Mark in Files dialog, in the same way than the Find in Files dialog. So, we could just bookmark any <!-- PART1 -->•••••••<!-- PART2 --> area, in all HTML files of folder 2, with, of course, the option Purge for each search ticked

                  • A Bookmark in Files dialog, too. So, we could :

                    • Select and copy the specific <!-- PART1 -->•••••••<!-- PART2 --> section of File1.html in the clipboard with Ctrl + C

                    • Then, run the Search > Mark > Paste to (Replace) bookmarked lines option, in all HTML files of folder 2

                  Best Regards,

                  guy038

                  A 1 Reply Last reply Apr 27, 2021, 11:54 AM Reply Quote 0
                  • R
                    Robin Cruise
                    last edited by Apr 27, 2021, 6:14 AM

                    This post is deleted!
                    1 Reply Last reply Reply Quote 0
                    • R
                      Robin Cruise
                      last edited by Apr 27, 2021, 6:43 AM

                      thanks @guy038 Beautiful !

                      1 Reply Last reply Reply Quote 0
                      • G
                        guy038
                        last edited by guy038 Apr 27, 2021, 9:22 AM Apr 27, 2021, 9:20 AM

                        Hello, @robin-cruise, @terry-r, @alan-kilborn, @peterjones and all,

                        Well, I just realize this morning, that I’m wrong again, in my previous post. :-((

                        Indeed, as several consecutive lines would be marked, the Search > Mark > Paste to (Replace) bookmarked lines option would replace any bookmarked line with clipboard contents ! whereas the paste action should occur, once only, replacing all the lines bookmarked !

                        As a conclusion, this kind of operation, between files, cannot be achieved with regexes, as @peterjones and @alan-kilborn stated, previously !

                        BR

                        guy038

                        1 Reply Last reply Reply Quote 0
                        • A
                          Alan Kilborn @guy038
                          last edited by Apr 27, 2021, 11:54 AM

                          @guy038 said in Regex: Copy text between one html file to more html files (from a folder to another):

                          has these two new native features ( just a dream !! )

                          Keep in mind that these “dream” features would have to be “Mark in All Opened Documents” and “Bookmark in All Opened Documents”…and thus the files in question would all have to be open in Notepad++.

                          The reason is that bookmarks and marks from marking are not saved with a file, so if a file was not open, there would be no remembering.

                          I know it was just a fantasy, but sometimes we have to do a little grounding of fantasy. :-)

                          1 Reply Last reply Reply Quote 1
                          • G
                            guy038
                            last edited by Apr 27, 2021, 1:21 PM

                            Hi, @alan-kilborn and All,

                            I really should have thought a bit more, before answering ! Indeed, Alan, although the bookmarks are kept in the session.xml configuration file, there are not stored in files themselves. So, as soon as a file containing bookmarks is closed, these bookmarks are gone away the next time the file is opened, during a new N++ session !

                            Sorry for all that noise !

                            Cheers,

                            guy038

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