Search code examples
windowspowershellpowershell-3.0azure-powershellpowershell-4.0

Powershell: Combine multiple html files into one single html file


I want to select and combine the content of all html files, from Folder 1, to a single html File from Folder 2.

Can this be done with PowerShell ?


Solution

  • to avoid problem of memory if you have lot of files:

    1. i suggest you to use a streamer:
    2. for html file, keep the utf8 encoding or you risk to loose some characters

    $rootFolder = "c:\Folder1"
    $outfile    = Join-Path -Path $rootFolder -ChildPath 'newfile.html'
    
    $sw = New-Object System.IO.StreamWriter $outfile, $true  # $true is for Append
    Get-ChildItem -Path $rootFolder -Filter '*.html' -File | ForEach-Object {
        Get-Content -Path $_.FullName -Encoding UTF8 | ForEach-Object {
            $sw.WriteLine($_)
        }
    }
    $sw.Dispose()