PowerShell: Replacing Characters Inside Text Files


function updateFile{
    if($file -ne $output){
        write-host "Processing $file`r`n=> updating $output"
        write-host "Processing $file"
    $content=get-content $file  # This avoids the default the get-content as an array of strings
    [System.IO.File]::WriteAllText($output,$($newContent|out-string)) # peruse the dotnet framework, which is independent of PoSh versions

    # This alternate method is to create an ISO-8859-1 encoded file.
    # It's not fully compatible with the most widely used legacy Windows code page, Windows-1252
    # The solution will not work with characters not valid for ISO-8859-1,
    # such as €: The following command breaks, for instance: Set-Content test.txt ([byte[]][char[]] "€") -Encoding Byte
    # Set-Content $file -Force ([byte[]][char[]] $newContent) -Encoding Byte
    # the -nonewline directive only works with PoSH v3+
    # $newContent|out-string|Set-Content -NoNewline $output -force

# One of the fastest methods to obtain list of files inside a directory
$files=(get-childitem $textFilesDirectory -recurse | where {$_.extension -in $textFileExtensions}).FullName
$files|%{updateFile $_ $regexNewlineOnly $replaceWith}

Posted on Categories Codes

Leave a Reply

Your email address will not be published. Required fields are marked *