【问题标题】:Filtering unknown character for OneDrive upload过滤 OneDrive 上传的未知字符
【发布时间】:2018-04-02 06:20:52
【问题描述】:

目前正在使用一个小的 powershell 脚本来检查文件结构中的非法字符,但是我遇到了一个我不熟悉的字符。我需要修改代码以检测和替换该字符是(或代表)的任何内容,但我无法实际键入该字符,它仅在文件资源管理器中查看时显示。该字符显示为方形项目符号,但如果我将其复制/粘贴到任何地方,它将变为带有问号的框。有人知道这个角色的alt代码吗?或者我可以用来检测和替换它的替代线?

需要进入的代码如下:

function OneDrive-Check($Folder,[switch]$Fix){
    $Items = Get-ChildItem -Path $Folder -Recurse

    $UnsupportedChars = '[!&{}~#%]'

    foreach ($item in $items){
        filter Matches($UnsupportedChars){
        $item.Name | Select-String -AllMatches $UnsupportedChars |
        Select-Object -ExpandProperty Matches
        Select-Object -ExpandProperty Values
        }

        $newFileName = $item.Name
        Matches $UnsupportedChars | ForEach-Object {
            Write-Host "$($item.FullName) has the illegal character $($_.Value)" -ForegroundColor Red
            if ($_.Value -match "&") { $newFileName = ($newFileName -replace "&", "and") }
            if ($_.Value -match "{") { $newFileName = ($newFileName -replace "{", "(") }
            if ($_.Value -match "}") { $newFileName = ($newFileName -replace "}", ")") }
            if ($_.Value -match "~") { $newFileName = ($newFileName -replace "~", "-") }
            if ($_.Value -match "#") { $newFileName = ($newFileName -replace "#", "") }
            if ($_.Value -match "%") { $newFileName = ($newFileName -replace "%", "") }
            if ($_.Value -match "!") { $newFileName = ($newFileName -replace "!", "") }
         }
         if (($newFileName -ne $item.Name) -and ($Fix)){
            Rename-Item -LiteralPath $item.FullName -NewName ($newFileName)
            Write-Host "$($item.Name) has been changed to $newFileName" -ForegroundColor Green
         }
    }
}

人物图片如下,日期两侧:

Unknown Illegal Character

谢谢!

【问题讨论】:

  • 您是否尝试更改您的代码页? windows命令行:chcp
  • @f14284 代码页提示引导我走上正确的道路,现在解决了,谢谢!
  • 好吧,不客气。

标签: powershell onedrive


【解决方案1】:

更改系统代码页可能会使您免于编写过滤器功能。使用良好的代码页尝试 'chcp' windows 命令行。

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/chcp

【讨论】:

    【解决方案2】:

    我不会尝试将非法字符匹配为[string],而是尝试将非法字符识别为 unicode 字符代码。

    对于该示例,首先您的文件名类似于:

    $name = $item.FullName
    $name[0..$name.Length] | % {
        "$_ = \u$(([int64]$_).ToString('X4'))"
    }
    

    所以你会得到实际的 unicode 字符:

    a = \u0061
    k = \u006B
    o = \u006F
    s = \u0073
    ...
    

    然后根据指定为 \u0000 的 unicode 字符代码过滤/替换结果作为 unicode 字符的十六进制代码,类似于:

    $String -replace '\u005A\u0061\u007A', ''
    

    在这里你可以找到一些关于实际 unicode 替换的好例子:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      相关资源
      最近更新 更多