0

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).

Angel_Boy
  • 958
  • 2
  • 7
  • 16
  • possible duplicate of [Powershell: Find/Replace pattern of ASCII control characters](http://stackoverflow.com/questions/15343442/powershell-find-replace-pattern-of-ascii-control-characters) – Victor Zakharov Oct 01 '13 at 16:01

1 Answers1

1

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)".

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Well I wouldn't say it doesn't work for any ASCII code. "{backtick}0" works for null or chr(0), "{backtick}a" for chr(7), "{backtick}b" for chr(8) and "{backtick}f" for chr(12). I would agree that it doesn't work for all ASCII codes. :-) – Keith Hill Oct 01 '13 at 17:27
  • Bad English on my part. I meant that you cannot use backtick to specify arbitrary ASCII codes. And you just covered this part nicely. Here, have a +1. :) – Victor Zakharov Oct 01 '13 at 18:44