首页 > 解决方案 > 递归替换win10文件夹和文件中的非标准字符

问题描述

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

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

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

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

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

标签: powershellwindows-10asciiansibatch-rename

解决方案


这个怎么样?替换特定字符,然后替换空格和波浪号或制表符之间的任何内容,例如 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".

推荐阅读