Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

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

    Help wanted · · · – – – · · ·
    4
    8
    113
    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.
    • Robin Cruise
      Robin Cruise last edited by

      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
      • Robin Cruise
        Robin Cruise last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • Robin Cruise
          Robin Cruise last edited by Robin Cruise

          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
          • guy038
            guy038 last edited by guy038

            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
            • Robin Cruise
              Robin Cruise last edited by

              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
              • Vasile Caraus
                Vasile Caraus last edited by Vasile Caraus

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • Vasile Caraus
                  Vasile Caraus last edited by

                  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
                  
                  Alan Kilborn 1 Reply Last reply Reply Quote 2
                  • Alan Kilborn
                    Alan Kilborn @Vasile Caraus last edited by

                    @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
                    • First post
                      Last post
                    Copyright © 2014 NodeBB Forums | Contributors