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



  • 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 ?



  • This post is deleted!


  • 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++ ?



  • 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



  • 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



  • This post is deleted!


  • 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
    


  • @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.


Log in to reply