从文件名中删除特殊字符的脚本
我有一个包含大量文件的文件夹。许多文件名都有“%”和/或“&”其中的人物。
e.g. Test&doc.pdf
e.g Test%doc.doc
有没有一种快速方法可以删除“%”和“&”使用 Windows 批处理文件、vbscript 或类似文件的字符?
我们将不胜感激所有帮助。
谢谢。
I have a folder containing a large number of files. A lot of the filenames have '%' and/or '&' characters in them.
e.g. Test&doc.pdf
e.g Test%doc.doc
Is there a quick way I could remove the '%' and '&' characters using a windows batch file, vbscript or something similar?
All help will be greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是批量执行此操作的方法(如果您好奇的话)。最大的限制是,如果文件名的符号超过百分之一,则它将不起作用,因为 shell 会将其扩展为变量。我不知道如何立即解决这个问题。
它从脚本所在的任何目录开始,并递归地处理所有子目录。
例如,打印输出如下:
Here's how you can do it in batch (in case you're curious). The big limitation is that if you have filenames with more than one percent sign, it won't work because the shell expands it to a variable. I don't know immediately how to fix that.
It starts from whatever directory the script is in, and works recursively over all subdirectories.
E.g., prints output like this:
@indiv
好的,我会尝试的。
诀窍是,切换延迟扩展,因为 for-loop-vars (%%N) 的扩展应该在没有延迟扩展的情况下完成,否则您会丢失感叹号,并遇到插入符号问题。
但要处理和修改字符串,您应该使用延迟扩展。
但为什么?答案是了解批处理解析器的各个阶段。
我试图在这里解释一下。
how-does-the-windows-命令解释器-cmd-exe-解析脚本
@indiv
Ok, I'll try.
The trick is, to toggle the delayed expansion, because expanding of for-loop-vars (%%N) should be done without delayed expansion, else you lose the exclamation marks, and got problems with carets.
But to handle and modify the strings you should use delayed expansion.
But why? The answer is to understand the phases of the batch parser.
I tried to explain it here.
how-does-the-windows-command-interpreter-cmd-exe-parse-scripts
我很快就把它们放在一起,但没有测试它,但这个 VBScript 应该可以解决问题。告诉我您是否需要文件夹递归替换等奇特的东西。
I've quickly thrown that together and didn't test it, but this VBScript should do the trick. Tell me if you need fancy stuff like folder recursive replacing etc.