• Login
Community
  • Login

Regex: Parsing, How to make a regex replacement in multiple files, from Folder-1 to Folder-2

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
8 Posts 4 Posters 472 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 Feb 13, 2021, 5:37 AM

    ultima-copy.png

    hello, the image will explain what I want to do. I have a lot of html files in Folder-1 and the same number of files in Folder-2. The name of the files is the same on both folders, only the content of them is different. As you can see in this image what I want to do is to copy the lines from <!–START–> to <!–FINNISH --> and paste them to other files from second folder, in html files, in the point PUT-HERE-THE-CODE

    You have to know that the content of <!–START–> to <!–FINNISH --> is different on all pages, so only this 2 words are repeated in all files, the links are different. And in the Folder-2, the only part that repeats in all files is PUT-HERE-THE-CODE

    SOLUTION. I made a regex for this task: This will select the lines between <!--START--> to <!--FINNISH -->

    FIND: (?s)(<\!--START-->).*(<\!--FINNISH -->)

    But how can I move this content in other files, from Folder-1 to Folder-2 to other file over the word PUT-HERE-THE-CODE ?

    1 Reply Last reply Reply Quote 0
    • R
      Robin Cruise
      last edited by Feb 13, 2021, 6:02 AM

      This post is deleted!
      1 Reply Last reply Reply Quote 0
      • R
        Robin Cruise
        last edited by Robin Cruise Feb 13, 2021, 7:46 AM Feb 13, 2021, 7:46 AM

        to make an idea, should be something like:

        FIND: (?s)(START).*(FINNISH) \ Folder-1
        Replace on: $1 PUT-HERE-THE-CODE $2 \ Folder-2

        Is it possible with notepad++ ?

        1 Reply Last reply Reply Quote 0
        • G
          guy038
          last edited by guy038 Feb 13, 2021, 10:37 AM Feb 13, 2021, 10:34 AM

          Hello @robin-cruise and All,

          I did not investigate, yet but I thought of some points :

          • Are there several PUT-HERE-THE-CODE lines in the files, located in Folder-2 ? In other words, do you have to paste the <!–START–> •••••• <!–FINNISH --> block in several locations of a same file ?

          • Are there several <!–START–> •••••• <!–FINNISH --> blocks in files, located in Folder-1 ? I suppose not because your regex would not match these sections properly and you would need several marks as PUT-HERE-THE-CODE_1, PUT-HERE-THE-CODE_2 and so on…

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 0
          • R
            Robin Cruise
            last edited by Feb 13, 2021, 12:37 PM

            yes, all files from Folder-1 have the same block <!–START–> •••••• <!–FINNISH --> but with different content. And all files from Folder-2 have the same word/line PUT-HERE-THE-CODE

            1 Reply Last reply Reply Quote 0
            • V
              Vasile Caraus
              last edited by Vasile Caraus Feb 13, 2021, 2:08 PM Feb 13, 2021, 2:06 PM

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • V
                Vasile Caraus
                last edited by Feb 13, 2021, 2:37 PM

                I don’t know if it can be done just with regex and notepad++. Maybe in the future. I know that in Sublime-Text this task can be done with a plugin made in Python.

                But, instead, you can use very easy PowerShell for this task.

                This solution is very simple. Just copy this code in PowerShell from Windows10

                $sourceFiles = Get-ChildItem 'c:\Folder1'  
                $destinationFolder = 'c:\Folder2'
                
                foreach ($file in $sourceFiles) {
                
                $sourceContent = Get-Content $file.FullName -Raw
                $contentToInsert = [regex]::match($sourceContent,"(?ms)<!--START-->(.+)<!--FINNISH -->").value
                $destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
                $destinationContent = $destinationContent -replace 'PUT-HERE-THE-CODE',$contentToInsert
                
                Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8
                
                } #end foreach file
                
                A 1 Reply Last reply Feb 14, 2021, 12:25 AM Reply Quote 2
                • A
                  Alan Kilborn @Vasile Caraus
                  last edited by Feb 14, 2021, 12:25 AM

                  @Vasile-Caraus

                  A viable technique, although not a technique inside Notepad++.

                  I think your technique is going to also copy over the “START” and “FINNISH” lines into the destination file, which, to the OP, may not be desirable.

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