从文件名中删除特殊字符的批处理文件脚本 (Windows) [英] Batch file script to remove special characters from filenames (Windows)

查看:46
本文介绍了从文件名中删除特殊字符的批处理文件脚本 (Windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大堆文件,其中一些在文件名中包含特殊字符(例如 ä、ö、% 等).我想要一个脚本文件来迭代这些文件并重命名它们以删除特殊字符.我真的不介意它做什么,但它可以用下划线替换它们,例如

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 将重命名为 St_rung_20.doc

Störung%20.doc would be renamed to St_rung_20.doc

按优先顺序:

  1. Windiws 批处理文件
  2. 使用 cscript (vbs) 运行的 Windows 脚本文件
  3. 可以从命令行运行的第三方软件(即无需用户交互)
  4. 另一个语言脚本文件,我必须为它安装一个额外的脚本引擎

背景:我试图在 Windows 上使用 GnuPG 加密这些文件,但它似乎无法使用 --encrypt-files 选项处理文件名中的特殊字符.

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.

推荐答案

感谢 Tomalak,他确实为我指明了正确的方向.我以为我会在这里发帖以保持完整性.

Thanks to Tomalak who actually pointed me in the right direction. Thought I'd post here for completeness.

问题似乎是 GPG 使用的代码页是固定的(拉丁文 I),独立于控制台中配置的代码页.但是一旦他指出了这一点,我就想出了如何解决这个问题.

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.

诀窍是在生成文件列表之前更改代码页.这实际上会使在控制台中查看时文件列表看起来不正确.但是,当传递给 GPG 时,它工作正常.GPG 接受文件并以正确的文件名吐出加密文件.

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.

批处理文件如下所示:

chcp 1252
dir /b /s /a-d MyFolder >filelist.txt
gpg -r test@test.com --encrypt-files <filelist.txt

这篇关于从文件名中删除特殊字符的批处理文件脚本 (Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆