Poweshell: instead pull out each file separately, I gather them all in the same file (that has all the names of the files)

hello. I made this PowerShell code. It will select all from <!-- ARTICOL START --> to  <!-- ARTICOL FINAL --> from every html files from Folder 1, and after modify will copy all those files into Folder 2 keeping the same name. So, my problem is the output.

In Folder 1 I have:

File.html

File2.html

File3.html

In Folder 2 I must have the same output names, different files, instead of this, the output will is one single file:

file-2.html file-3.html file.html

$file = Get-ChildItem c:\Folder1\*.html
$outfile = "c:\Folder2\"
[email protected]()
$content = Get-Content -Path $file
$start = $content | Where-Object {$_ -match '<!-- ARTICOL START -->'}
$final = $content | Where-Object {$_ -match '<!-- ARTICOL FINAL -->'}
for($i=0;$i -lt $content.Count;$i++){
if(($i -gt $content.IndexOf($start)) -and ($i -lt $content.IndexOf($final))){
if($content[$i] -notmatch '<p class='){
continue
}
}
$output += $content[$i]
}
$output | Out-File -FilePath $resultsdir\$($file.name)

  • SHOULD BE:


    $sourcedir
    = "C:\Folder1\" $resultsdir = "C:\Folder2\" Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object{ $output[email protected]() $content = Get-Content -Path $_.FullName $start = $content | Where-Object {$_ -match '<!-- ARTICOL START -->'} $final = $content | Where-Object {$_ -match '<!-- ARTICOL FINAL -->'} for($i=0;$i -lt $content.Count;$i++){ if(($i -gt $content.IndexOf($start)) -and ($i -lt $content.IndexOf($final))){ if($content[$i] -notmatch '<p class='){ continue } } $output += $content[$i] } $output | Out-File -FilePath $resultsdir\$($_.name) }