PARSING: Copy lines from several files into a single point of other html files

hello, maybe someone can help me. 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 --> comments 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

 

This are the code lines from both files:

https://justpaste.it/7r6a2

and

https://justpaste.it/3el6h

 

 

You can use a regular expression to grab everything between <!–START–> and <!–FINNISH --> and just replace the text in the other file. Here’s an example for a single file. Put this into a loop for all your files and you’re good to go:

$source = Get-Content E:\Temp\Files\HTML\html-source.htm -Raw

$destination = Get-Content E:\Temp\Files\HTML\html-dest.htm -Raw

$newContent = [regex]::match($source,"<!--START-->[\s\S]+<!--FINNISH -->").value

$destination = $destination -replace 'PUT-HERE-THE-CODE',$newContent

Set-Content E:\Temp\Files\HTML\html-dest.htm $destination -Encoding UTF8

 

Suzana,
welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: "Read Me Before Posting! You’ll be Glad You Did!"
Then ... when you post code, error messages, sample data or console output format it as code, please.
Here you can read how that works: Guide to Posting Code
Do not post images of code as this is not helpful in most cases.
If you don't want to post your code directly you can use a GitHub GIST and post the link here.
This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts
or solutions on request.
What have you tried so far? We expect you to make an own attempt to get your task done or to solve your problem. If you
have done so already please document here what exactly you have done and show your code. Then we probably might be able
to help you step further.

You’re just too nice! :wink: :smiley:

Thank you, yes, but your code will make a replacement only in 2 files, html-source.htm and html-dest.htm But I have 1300 files in Folder-1 and 1300 files in Folder-2. So I want to change all at once. How can I do that ?

I made a PowerShell Code, but is not working. Where did I go wrong?

https://pastebin.com/kJF3gp0j

 

That was just an example to get you started. You need to put the code in a loop to process all the files. Here’s another example:

$sourceFiles = Get-ChildItem E:\Temp\Files\HTML\source  
$destinationFolder = 'E:\Temp\Files\HTML\destination'

foreach ($file in $sourceFiles) {

    $sourceContent = Get-Content $file.FullName -Raw
    $contentToInsert = [regex]::match($sourceContent,"<!--START-->[\s\S]+<!--FINNISH -->").value
    $destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
    $destinationContent = $destinationContent -replace 'PUT-HERE-THE-CODE',$contentToInsert

    Set-Content -Path $destinationFolder\$($file.Name) -Value $destination -Encoding UTF8

} #end foreach file

 

bump

Thank for reply, I believe the more appropriate regex would be (?s)(<\!--START-->).*(<\!--FINNISH -->)
I write again your code
$sourceFiles = Get-ChildItem 'c:\Folder1' $destinationFolder = 'c:\Folder2'

foreach ($file in $sourceFiles) {

$sourceContent = Get-Content $file.FullName -Raw
$contentToInsert = [regex]::match($sourceContent,"(?s)(<!–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 $destination -Encoding UTF8

} #end foreach file

the problem is, after run the code, all of my files are empty, so something deleted everything on my files. I do’t know what the problem can be…

 

Sorry, my fault for changing the variable names. The last line wasn’t fully changed from the previous example, the value should be $destinationContent

Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8

I note you’ve also modified the regular expression. Your one doesn’t match any content when I test it.

WORKS PERFECTLY. Thanks. Also, works with this regex: (?ms)<!–START–>(.+)<!–FINNISH -->

 

$sourceFiles = Get-ChildItem ‘c:\Folder-1’
$destinationFolder = ‘c:\Folder-2’

foreach ($file in $sourceFiles) {

$sourceContent = Get-Content $file.FullName -Raw
$contentToInsert = [regex]::match($sourceContent,"<!–START–>[\s\S]+<!–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