question

SuzanaEree-2102 avatar image
0 Votes"
SuzanaEree-2102 asked AndreasBaumgarten commented

PowerShell: Copy the content of one file to other several files with regex

people. I have in Folder1 a html file named file1.html that has this content:

Folder1 (file1.html)

 <!-- FLAGS_1 -->
    <div class="cautareField">
           <div align="right">
              <a href="https://neculaifantanaru.com/izolarea-cercetatorului-in-cursa-lunga-a-leadershipului.html">&nbsp; <a href="https://neculaifantanaru.com/fr/l-isolement-du-chercheur-dans-le-marathon-de-leadership.html">&nbsp; <a href="https://neculaifantanaru.com/en/the-flashing-of-a-mind-inclined-towards-the-infinite.html">
          </div>
         </div>
 <!-- FLAGS -->

Folder2 (filexxx.html)

 <!-- FLAGS_1 -->
   <div class="cautareField">
       <div align="right">
          <a href="https://neculaifantanaru.com/index.html">&nbsp; <a href="https://neculaifantanaru.com/fr/index.html">&nbsp; <a href="https://neculaifantanaru.com/en/index.html">
      </div>
     </div>
 <!-- FLAGS -->

And I want to move this content from <!-- FLAGS_1 --> to <!-- FLAGS --> in the same place on other several html files located in Folder2, that has different names. The only problem is that my code fails to make the copy, it only moves file1.html to Folder2 that has different names, but the same structure (only the links are different). Don't know why.


 $sourceFiles = Get-ChildItem -Path "c:\Folder1" -Filter "*.html";  
 $destinationFolder = 'c:\Folder2'
    
 foreach ($file in $sourceFiles) {
    
     $sourceContent = Get-Content $file.FullName -Raw
     $contentToInsert = [regex]::match($sourceContent,"(?ms)<!-- FLAGS_1 -->(.+)<!-- FLAGS -->").value
     $destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
     $destinationContent = $destinationContent -replace '(?ms)<!-- FLAGS_1 -->(.+)<!-- FLAGS -->',$contentToInsert
    
     Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8
    
 } #end foreach file


windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @SuzanaEree-2102 ,

is the script you posted the full script?

Here your script works like this:

  • Get all html files in folder 1

  • For reach file in files:

  • Get the file with the same name in folder 2

  • Replace the text between <!-- FLAGS_1 --> to <!-- FLAGS -->

  • Save file in folder 2

  • Next file of folder 1

I don't understand what should happen.


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten




0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten commented

Hi @SuzanaEree-2102 ,

please give this a try.

 # Sourcefile contains text to insert
 $sourcefile = "./1/file1.html"
 # Get content to insert
 $sourceContent = Get-Content -Path $sourcefile  -Raw
 # Prepare regex
 $contentToInsert = [regex]::match($sourceContent, "(?ms)<!-- FLAGS_1 -->(.+)<!-- FLAGS -->").value
 # Get target files
 $destinationFiles = Get-ChildItem -Path "./2" -Filter "*.html";  
 # Do for each file in destination folder
 foreach ($file in $destinationFiles) {
     # Get content of destination file
     $destinationContent = Get-Content $file.FullName -Raw
     # Relace 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


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

works, THANK you sir !!

0 Votes 0 ·

Thanks for the feedback.


Kind regards
Andreas Baumgarten


0 Votes 0 ·
SuzanaEree-2102 avatar image
0 Votes"
SuzanaEree-2102 answered SuzanaEree-2102 commented

hello sir @AndreasBaumgarten Yes, I update mu post so as to understand better my problem. Of course, I am not a programmer/developer and I don't know very well Powershell.

So, I need to get/copy the content of the file1.html from Folder1 (with regex, that works fine) and paste it in filexxx.html, filexxx-1.html, filexxx-2.html..and so on.

Basilcay, I need to copy content from <!-- FLAGS_1 --> to <!-- FLAGS --> from file1.html and paste it to other html files from the second folder, also from <!-- FLAGS_1 --> to <!-- FLAGS -->

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Now I got it ... I hope ;-)

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?


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

0 Votes 0 ·

YES, exactly, this is what I want to do

0 Votes 0 ·