问题描述
我想编写一个批处理脚本重命名文件,这是有它的文件名的特殊字符。
例如:我的文档%
如果这个例子是我的文件名,我怎么可以重命名使用批处理脚本呢?
I want to write a batch script to rename a file,which is having special characters in it's file name.Example: "My%Document"If this example is my file name ,how can i rename it using batch script?
推荐答案
有关在环境变量定义包含特殊字符的文件路径(或一般任意字符串),可使用设置
其中全部转让前pression是括在,
:
For defining a file path (or any string in general) containing special characters in an environment variable, use set
where the entire assignment expression is enclose in ""
:
rem DEFINE PATH: C:\Root\dir=sub\foo&bar^\100%!
set "FILEPATH=C:\Root\dir=sub\foo&bar^\100%%!"
的唯一的事情是你必须加倍所有%
到 %%
,如上面所示。
要任何命令中使用这样的文件路径(或一般字符串),你需要使用延迟扩展。这样,你傻瓜命令间preTER。
To use such a file path (or string in general) for any command, you need to use delayed expansion. That way you "fool" the command interpreter.
把所有这些组合起来,下面应该工作:
Putting all this together, the following should work:
rem DEFINE PATH: C:\Root\dir=sub\foo&bar^\100%!
set "FILEPATH=C:\Root\dir=sub\foo&bar^\100%%!"
rem DEFINE NEW NAME: new=name%
set "FILENEWN=new=name%%"
rem RENAME FILE
setlocal EnableDelayedExpansion
ren "!FILEPATH!" "!FILENEWN!"
endlocal
这篇关于批处理脚本在其文件名命名,它们是具有特殊字符的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!