如何使用powershell替换

时间:2013-10-01 15:35:20

标签: vb.net powershell scripting replace powershell-ise

这是我的vb代码。 这正是我想用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

我的Powershell脚本:

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
}

我正在尝试替换一些ascii字符控制代码(0到10)和(12到15)和(17)以及(21到30)。

1 个答案:

答案 0 :(得分:1)

在PowerShell中,转义字符不是\,因为它是有效的路径字符。使用反引号代替,例如“``N”。对于任意ASCII代码,使用“$([char] number)”,例如“$([炭] 2)”。