【问题标题】:How to Replace using powershell如何使用 powershell 进行替换
【发布时间】:2013-10-01 15:35:19
【问题描述】:

这是我的 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)。

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 好吧,我不会说它不适用于任何 ASCII 代码。 “{backtick}0”适用于 null 或 chr(0),“{backtick}a”适用于 chr(7),“{backtick}b”适用于 chr(8),“{backtick}f”适用于 chr(12) .我同意它不适用于所有 ASCII 代码。 :-)
  • 我的英语不好。我的意思是您不能使用反引号来指定任意 ASCII 代码。你刚刚很好地涵盖了这部分。在这里,有一个+1。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多