Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

1 Answer

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

share|improve this answer
Backtick does not work for any ASCII code. – Neolisk 2 days ago
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 2 days ago
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. :) – Neolisk 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.