【问题标题】:Powershell not printing accented characters correctlyPowershell 无法正确打印重音字符
【发布时间】:2016-08-10 22:03:06
【问题描述】:

所以我正在尝试使用 powershell 重命名文件,脚本应该删除所有特殊字符。

简单吧?没那么快。重音字符未正确解析

这是我正在使用的脚本,名为renamefiles.ps1

param([string]$complfn) #Must be the first statement in your script

尝试了这两种方法,没有任何区别,要么使用其中之一,要么不使用

#$OutputEncoding = [Console]::OutputEncoding
#[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
write-host $complfn

$encodage = New-Object System.Text.ASCIIEncoding 
[System.Console]::OutputEncoding = $encodage
Write-Host $complfn

$encodage = New-Object System.Text.utf8encoding 
[System.Console]::OutputEncoding = $encodage
Write-Host $complfn

$newcomplfn  = [Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding("Cyrillic").GetBytes($complfn))
Write-Host $newcomplfn

我是这样使用的 powershell.exe -file renamefiles.ps1 -complfn "ásdfésdf.txt"

这是我得到的输出:

ásdfésdf.txt
ásdfésdf.txt
ásdfésdf.txt
asdfesdf.txt

我看到最后一个命令有效地替换了重音字符,但是你知道我可以用什么来显示原始参数(文件名)吗?

【问题讨论】:

  • powershell.exe -file renamefiles.ps1 -complfn "ásdfésdf.txt" 你是从什么做的?从命令行?来自 PowerShell 脚本文件 (.ps1)?批处理文件(.bat .cmd)?
  • @PetSerAl 来自 ISE,它位于 ps1 文件中,但我的目标是从右键单击命令菜单(可能是 .bat 文件)运行此脚本
  • renamefiles.ps1 脚本之前和内部执行[Console]::OutputEncoding=[Text.Encoding]::Unicode
  • @PetSerAl 感谢您的帮助。我试过了,但没有运气。我正在使用 win10x64
  • 你需要看角色吗? foreach ($c in [char[]]"ásdfésdf.txt") { Write-Output ([int]$c) } 将为您获取每个字符的代码;对照一系列已知的“好”字符进行检查?

标签: powershell utf-8 ascii


【解决方案1】:

我通过在记事本++中设置ANSI格式而不是UTF-8(无BOM)解决了这个问题

【讨论】:

    【解决方案2】:

    我通过在脚本中添加chcp 65001 解决了这个问题

    这会将代码页更改为 Unicode

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2019-10-15
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多