In PowerShell the escape character is not \
because that is a valid path character. Use a backtick instead e.g. "``n". For arbitrary ASCII codes use "$([char]number)" e.g. "$([char]2)".
How to Replace using powershell
-
30-06-2022 - |
Question
Here is my vb code. This is exactly what i want to do with powershell.
Public Sub Main()
Dim file As New System.IO.StreamReader(Dts.Variables("User::str_SourcePath").Value.ToString())
Dim data As String
data = file.ReadToEnd()
data = data.Replace("""" & vbCrLf, """" & vbLf)
data = data.Replace(vbCrLf, " ")
data = data.Replace(vbCr, vbLf)
data = data.Replace(Chr(32) & vbCrLf, vbLf)
data = data.Replace(Chr(32) & vbLf, vbLf)
data = data.Replace("'", "")
data = data.Replace(Chr(0), "")
file.Close()
Dim writer As New System.IO.StreamWriter(Dts.Variables("User::str_SourcePath").Value.ToString(), False)
writer.Write(data)
writer.Flush()
writer.Close()
Dts.TaskResult = ScriptResults.Success
End Sub
My Powershell Script:
Get-ChildItem "C:\Users\abc\Desktop\Files" | ForEach-Object {
$Content = Get-Content $_.fullname
$Content = ForEach-Object { $Content -replace "\r\n","\n" }
$Content = ForEach-Object { $Content -replace "'","" }
$Content = ForEach-Object { $Content -replace "chr(0)","" }
Set-Content $_.fullname $Content -Force
}
I am trying to replace a few ascii characters control code (0 to 10) and from (12 to 15) and (17) and also from (21 to 30).
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow