用于从文件名中删除特殊字符的批处理文件脚本 (Windows)
我有一大堆文件,其中一些文件名中包含特殊字符(例如 ä、ö、% 等)。 我想要一个脚本文件来迭代这些文件并重命名它们以删除特殊字符。 我真的不介意它的作用,但它可以用下划线替换它们,例如
Störung%20.doc 将重命名为 St_rung_20.doc
按优先顺序:
- Windiws 批处理文件
- 使用 cscript 运行的 Windows 脚本文件( vbs)
- 可以从命令行运行的第三方软件(即不需要用户交互)
- 另一种语言脚本文件,为此我必须安装额外的脚本引擎
背景:我正在尝试加密这些在 Windows 上使用 GnuPG 文件,但它似乎无法使用 --encrypt-files 选项处理文件名中的特殊字符。
I have a large set of files, some of which contain special characters in the filename (e.g. ä,ö,%, and others). I'd like a script file to iterate over these files and rename them removing the special characters. I don't really mind what it does, but it could replace them with underscores for example e.g.
Störung%20.doc would be renamed to St_rung_20.doc
In order of preference:
- A Windiws batch file
- A Windows script file to run with cscript (vbs)
- A third party piece of software that can be run from the command-line (i.e. no user interaction required)
- Another language script file, for which I'd have to install an additional script engine
Background: I'm trying to encrypt these file with GnuPG on Windows but it doesn't seem to handle special characters in filenames with the --encrypt-files option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在将文件名提供给 gnupg 之前,您是否尝试过将 cmd.exe 设置为另一个代码页? 发出 chcp 65001 预先将 cmd.exe 设置为 Unicode,然后重试。
如果失败,下面的 VBScript 将执行此操作:
如下调用:
您还可以在 Windows 资源管理器中将文件夹拖放到该文件夹上。
Have you tried setting cmd.exe into another codepage before you feed the file names to gnupg? Issue
chcp 65001
to set cmd.exe to Unicode beforehand and try again.If that fails, the following VBScript would do it:
call it like this:
You can also drag&drop a folder onto it in Windows Explorer.
来自http://www.robvanderwoude.com/bht.html:
使用NT的SET的字符串替换替换或删除字符串中任意位置的字符:
如果您使用此尝试,您将必须处理要替换的每个字符(例如 ä,Ö,Ü,ä,ö,ü,ß,还有 á,à,é ,è...) 分别。
From http://www.robvanderwoude.com/bht.html:
use NT's SET's string substitution to replace or remove characters anywhere in a string:
If you use this attempt, you will have to process each character you want to replace (e.g. Ä,Ö,Ü,ä,ö,ü,ß, but also á,à,é,è...) seperately.
感谢托马拉克,他实际上为我指明了正确的方向。 为了完整起见,我想在这里发帖。
问题似乎是 GPG 使用的代码页是固定的(拉丁 I),独立于控制台中配置的代码页。 但一旦他指出了这一点,我就想出了如何解决这个问题。
诀窍是在生成文件列表之前更改代码页。 这实际上会使文件列表在控制台中查看时显得不正确。 然而,当传递给 GPG 时,它工作得很好。 GPG 接受文件并输出具有正确文件名的加密文件。
批处理文件看起来像这样:
Thanks to Tomalak who actually pointed me in the right direction. Thought I'd post here for completeness.
The problem seems to be that the codepage used by GPG is fixed (Latin I) independent of the codepage configured in the console. But once he pointed this out, I figured out how to workaraound this.
The trick is to change the codepage before generating the file list. This will actually make the filelist appear to be incorrect when viewed in the console. However, when passed to GPG, it works fine. GPG accepts the files and spits out the encrytped files with correct filenames.
The batch file looks something like this:
遵循“RenameFilesWithAccentedAndDiariticLatinChars.pl”PERL 脚本重命名带有重音和变音拉丁字符的文件:
当前文件夹。
CP 1250、CP 1252、CP 1254 和 CP 1257(主要是带重音的拉丁字符)
或带有变音符号的拉丁字符。
仅来自拉丁字符(例如,Été --> Ete)。
以获得全球批准。
选项“--batch”避免交互式问题。 小心使用。
选项“--”避免下一个参数被解释为选项。
特别警告:
可能会发现名称还包含一些具有旧编码的字符。
检测和使用适当的编码。
所以这个脚本应该只通过删除重音符号和变音符号来重命名文件
拉丁字符的标记。
(Windows、Mac OS X、Linux)和有限数量的终端编码
(CP 850、ISO-8859-1、UTF-8)。
随机名称。
注意(注意内部字符串、输入、输出和命令的编码)
Following 'RenameFilesWithAccentedAndDiacriticalLatinChars.pl' PERL script renames files with accented and diacritical Latin characters :
the current folder.
CP 1250, CP 1252, CP 1254 and CP 1257 (mostly accented Latin characters)
or Latin characters having diacritical marks.
only from Latin characters (For example, Été --> Ete).
for global approval.
Option '--batch' avoids interactive questions. Use with care.
Option '--' avoids the next parameter to be interpreted as option.
Special Warning :
names may be found to contain also some characters having legacy encoding.
detection and use of appropriate encoding.
So this script should only rename files by removing accents and diacritical
marks from Latin characters.
(Windows, Mac OS X, Linux) and a limited number of terminal encodings
(CP 850, ISO-8859-1, UTF-8).
random names.
care (beware encoding of internal strings, inputs, outputs and commands)
我正在使用这个批处理来重命名文件夹,到目前为止似乎工作正常......
在我的例子中,代码页是 1252,你的代码页可能会有所不同。
I'm using this batch to rename folders and seems to work fine so far...
In my case codepage is 1252, yours might be different.