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

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?

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

GOT IT. my regex formula was a little wrong. I put an extra $ :slight_smile:

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

 

$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

Glad you have it figured out.