Reputation:
people. I have in Folder1 a html file named file1.html that has this content:
Folder1
<!-- FLAGS_1 -->
<div class="cautareField">
<div align="right">
<a href="https://neculaifantanaru.com/izolarea-cercetatorului-in-cursa-lunga-a-leadershipului.html"> <a href="https://neculaifantanaru.com/fr/l-isolement-du-chercheur-dans-le-marathon-de-leadership.html"> <a href="https://neculaifantanaru.com/en/the-flashing-of-a-mind-inclined-towards-the-infinite.html">
</div>
</div>
<!-- FLAGS -->
Folder2 (example of the content of all files, so only the links are different)
<!-- FLAGS_1 -->
<div class="cautareField">
<div align="right">
<a href="https://neculaifantanaru.com/index.html"> <a href="https://neculaifantanaru.com/fr/index.html"> <a href="https://neculaifantanaru.com/en/index.html">
</div>
</div>
<!-- FLAGS -->
So the content between <!-- FLAGS_1 -->
to <!-- FLAGS -->
in file1.html needs be copied in all html files in folder 2 at the same <!-- FLAGS_1 -->
to <!-- FLAGS -->
place
Upvotes: 2
Views: 274
Reputation:
# Sourcefile contains text to insert
$sourcefile = "c:/Folder1/file1.html"
# Get content to insert
$sourceContent = Get-Content -Path $sourcefile -Raw
# Get target files
$destinationFiles = Get-ChildItem -Path "c:/Folder2" -Filter "*.html";
# Do for each file in destination folder
foreach ($file in $destinationFiles) {
# Prepare regex
$contentToInsert = [regex]::match($sourceContent,'(?ms)<!-- FLAGS_1 -->(.+)<!-- FLAGS -->').value
# Get content of destination file
$destinationContent = Get-Content $file.FullName -Raw
# Replace the text in destination file content
$destinationContent = $destinationContent -replace '(?ms)<!-- FLAGS_1 -->(.+)<!-- FLAGS -->',$contentToInsert
# Write back destination file
Set-Content -Path $file.FullName -Value $destinationContent -Encoding UTF8
} #end foreach file
Thank you, AndreasBaumgarten, who helped me here.
Upvotes: 1