-2

我有许多文件夹,其中包含从同事那里获得的具有特殊甚至隐藏字符的文件的子文件夹,例如:

非标准字符:{µ, 市, ', &, 「, 隐形字符, Ü, é, ... }

我正在寻找一个脚本或 Windows 工具,它可以通过根据列表 X 中的字符将任何非标准字符替换为标准字符来一次性重命名所有子文件夹和文件。奖励:如果该工具会更好将检查列表 R,并使用替换规则(如果已定义)。如果不是,它应该只用“_”替换非标准字符。

List X: {A-Z, a-z, 0-9, (, ), [,],-, _}
List R: {é->e , ü->u, ä->a, @->at,...} (Replacement rules)

感谢对工具或脚本的任何提示。

4

2 回答 2

4

这个怎么样?替换特定字符,然后替换空格和波浪号或制表符之间的任何内容,例如 unicode 雪人。

echo hi | set-content filä.txt,filé.txt,fil☃.txt # in script or ise
$pairs = ('ä','a'), ('é','e'), ('[^ -~\t]','_')
dir -r | where name -match '[^ -~\t]' | rename-item -newname {
  $name = $_.name
  foreach ($pair in $pairs) {
    $name = $name -replace $pair
  }
  $name
} -whatif


What if: Performing the operation "Rename File" on target "Item:
C:\users\admin\foo\filä.txt Destination: C:\users\admin\foo\fila.txt".
What if: Performing the operation "Rename File" on target "Item:
C:\users\admin\foo\filé.txt Destination: C:\users\admin\foo\file.txt".
What if: Performing the operation "Rename File" on target "Item:
C:\users\admin\foo\fil☃.txt Destination: C:\users\admin\foo\fil_.txt".
于 2021-02-06T21:13:42.853 回答
1

我会解析文件/目录名称,如果每个字符不在 ascii 范围内,则将其替换为任意字符。您可以使用以下内容,并使用哈希表为您的 List X 和 List R 项目构建它。

filter ConvertTo-FileSafe {
    $userString = [char[]]$_
    $charToReplaceWith = '.'
    for ($currentChar = 0; $currentChar -lt $userString.Length; $currentChar++) {
        if ([System.IO.Path]::GetInvalidFileNameChars().Contains($userString[$currentChar])){
            $userString[$currentChar] = $charToReplaceWith
        }
        $previousChar = $userString[$currentChar]
    }
    return ($userString -join '')
}
于 2021-02-06T20:35:44.807 回答