# Parsing: How to copy all lines from the beginning of the html file to the

**URL:** <https://forums.powershell.org/t/parsing-how-to-copy-all-lines-from-the-beginning-of-the-html-file-to-the/15644>\
**Category:** PowerShell Help\
**Created:** [February 14, 2021, 2:00am UTC](https://forums.powershell.org/t/parsing-how-to-copy-all-lines-from-the-beginning-of-the-html-file-to-the/15644 "2021-02-14T02:00:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![me-suzy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/me-suzy/32/4055_2.png) [@me-suzy](https://forums.powershell.org/u/me-suzy)\
**Post date:** [February 14, 2021, 2:00am UTC](https://forums.powershell.org/t/parsing-how-to-copy-all-lines-from-the-beginning-of-the-html-file-to-the/15644/1 "2021-02-14T02:00:38Z")

</div>

hello, how to copy all lines from the beginning of html file to the \<body\>, from Folder-1 to Folder-2, having the same number of files and the same names?

 ![](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/6/6ca6081942733d75104f8e03d0942ee0e7a9e016.png)

I use this Regex to select all from the beginning of file to \<body\> , as this **\A(.\*?)$[\s\S]+\<body\>**

This Code I update in PowerShell is not working. What is the mistake ?

$sourceFiles = Get-ChildItem ‘c:\Folder1’  
$destinationFolder = ‘c:\Folder2’

foreach ($file in $sourceFiles) {

$sourceContent = Get-Content $file.FullName -Raw  
$contentToInsert = [regex]::match($sourceContent,"\A(._?)$[\s\S]+(\<body\>)").value  
$destinationContent = Get-Content $destinationFolder$($file.Name) -Raw  
$destinationContent = $destinationContent -replace '\A(._)$[\s\S]+(\<body\>)',$contentToInsert

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

} #end foreach file

---

<div class="post-metadata">

**Author:** ![me-suzy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/me-suzy/32/4055_2.png) [@me-suzy](https://forums.powershell.org/u/me-suzy)\
**Post date:** [February 14, 2021, 2:24am UTC](https://forums.powershell.org/t/parsing-how-to-copy-all-lines-from-the-beginning-of-the-html-file-to-the/15644/2 "2021-02-14T02:24:41Z")

</div>

GOT IT. my regex formula was a little wrong. I put an extra `$` 🙂

Should be: **\A(._)[\s\S]+(\<body\>)_** _or **\A(.**_**)[\s\S]+(\<body\>)|\2**` (so, without $)

&nbsp;

$sourceFiles = Get-ChildItem ‘c:\Folder1’  
$destinationFolder = ‘c:\Folder2’

foreach ($file in $sourceFiles) {

$sourceContent = Get-Content $file.FullName -Raw  
$contentToInsert = [regex]::match($sourceContent,"\A(._)[\s\S]+(\<body\>)").value  
$destinationContent = Get-Content $destinationFolder$($file.Name) -Raw  
$destinationContent = $destinationContent -replace '\A(._)[\s\S]+(\<body\>)',$contentToInsert

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

} #end foreach file

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [February 15, 2021, 3:47am UTC](https://forums.powershell.org/t/parsing-how-to-copy-all-lines-from-the-beginning-of-the-html-file-to-the/15644/3 "2021-02-15T03:47:39Z")

</div>

Glad you have it figured out.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/parsing-how-to-copy-all-lines-from-the-beginning-of-the-html-file-to-the/15644/4 "2024-05-16T20:27:51Z")

</div>


