You probably have a file that is flagged as read only.
Downfiles = Get-ChildItem –Path $localPath
if ($Downfiles -ne $null) #download directory pocess
{
foreach($file in $Downfiles)
{
"Processing {0}" -f $file.fullname
$content = [System.IO.File]::ReadAllText($file.fullname).Replace("`n","~")
Set-ItemProperty -Path $file.fullname -Name IsReadOnly -Value $false
[System.IO.File]::WriteAllText($file.fullname, $content)
}
}