Community
    • 登入

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

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    8 貼文 4 Posters 655 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Robin CruiseR
      Robin Cruise
      最後由 編輯

      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 條回覆 最後回覆 回覆 引用 0
      • Robin CruiseR
        Robin Cruise
        最後由 編輯

        此回覆已被刪除!
        1 條回覆 最後回覆 回覆 引用 0
        • Robin CruiseR
          Robin Cruise
          最後由 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 條回覆 最後回覆 回覆 引用 0
          • guy038G
            guy038
            最後由 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 條回覆 最後回覆 回覆 引用 0
            • Robin CruiseR
              Robin Cruise
              最後由 編輯

              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 條回覆 最後回覆 回覆 引用 0
              • Vasile CarausV
                Vasile Caraus
                最後由 Vasile Caraus 編輯

                此回覆已被刪除!
                1 條回覆 最後回覆 回覆 引用 0
                • Vasile CarausV
                  Vasile Caraus
                  最後由 編輯

                  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 KilbornA 1 條回覆 最後回覆 回覆 引用 2
                  • Alan KilbornA
                    Alan Kilborn @Vasile Caraus
                    最後由 編輯

                    @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 條回覆 最後回覆 回覆 引用 1
                    • 第一個貼文
                      最後的貼文
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors