PowerShell: Replacing Characters Inside Text Files

$textFilesDirectory="C:\Users\$env:computername\Desktop\test"
$textFileExtensions='.txt'
$regexNewlineOnly="(?<!\r)\n$"
$replaceWith="`r`n"

function updateFile{
    param(
        $file="C:\Users\$env:username\Desktop\test.txt",
        $regexMatch="(?<!\r)\n$",
        $replaceWith="`r`n",
        $output
    )
    $output=if($output){$output}else{$file}
    if($file -ne $output){
        write-host "Processing $file`r`n=> updating $output"
    }else{
        write-host "Processing $file"
    }
    $content=get-content $file  # This avoids the default the get-content as an array of strings
    $newContent=$content|%{$_.Replace($regexMatch,$replaceWith)}
    [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}

Leave a Reply

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