Dos批处理文件标题大写并删除特殊字符
@Echo Off
SetLocal EnableDelayedExpansion
Set _ScrFldr=%~dp0
PushD %_ScrFldr%
:: You can add other extensions to do multiple file types
For /F "Delims=" %%A In ('Dir /A-D /B *.txt') Do (
:: The space after the = is needed so we can Proper Case the first word
Set _FileName= %%A
:: Remove (, replace ( with _, replace - with _-_ ,
:: replace _ with space, and a first pass at removing multiple spaces
For %%I In ("%%~xA=" ".= " ")=" "(=_" "-=_-_" "_= " " = " **"!="**) Do Call Set
"_FileName=%%_FileName:%%~I%%"
:: Now make it all lower case, and another pass at removing multiple spaces
For %%I In ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z" " = ") Do Call Set "_FileName=%%_FileName:%%~I%%"
:: Proper Case and again remove multiple spaces
For %%I In (" a= A" " b= B" " c= C" " d= D" " e= E" " f= F" " g= G" " h= H" " i= I" " j= J" " k= K" " l= L" " m= M" " n= N" " o= O" " p= P" " q= Q" " r= R" " s= S" " t= T" " u= U" " v= V" " w= W" " x= X" " y= Y" " z= Z" " = ") Do Call Set "_FileName=%%_FileName:%%~I%%"
:: Last check for multiple spaces
Call :RmLoop
Set _FileName=!_FileName: = !
:: Remove any trailing underscore
Set _FileName=!_FileName:_%%~xA=%%~xA!
Ren "%%A" "!_FileName:~1!%%~xA"
)
PopD
Goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::
:RmLoop
Call Set "_FileName=%%_FileName: = %%"
Echo !_FileName!|Findstr /C:" ">Nul
If !ErrorLevel!==0 Goto RmLoop
这应该做的是从文件名和标题大小写中删除某些字符,即
some_filename = Some Filename
filename number 2 = Filename Number 2
大多数工作正常,例如更改大小写和删除多余的空格和下划线
但我在处理特殊字符时遇到问题,例如 !
(代码向下 10 行) 如果文件名为 ie 文件! 2 它被忽略
尝试使用 "^!="
^"!="
但无济于事
@Echo Off
SetLocal EnableDelayedExpansion
Set _ScrFldr=%~dp0
PushD %_ScrFldr%
:: You can add other extensions to do multiple file types
For /F "Delims=" %%A In ('Dir /A-D /B *.txt') Do (
:: The space after the = is needed so we can Proper Case the first word
Set _FileName= %%A
:: Remove (, replace ( with _, replace - with _-_ ,
:: replace _ with space, and a first pass at removing multiple spaces
For %%I In ("%%~xA=" ".= " ")=" "(=_" "-=_-_" "_= " " = " **"!="**) Do Call Set
"_FileName=%%_FileName:%%~I%%"
:: Now make it all lower case, and another pass at removing multiple spaces
For %%I In ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z" " = ") Do Call Set "_FileName=%%_FileName:%%~I%%"
:: Proper Case and again remove multiple spaces
For %%I In (" a= A" " b= B" " c= C" " d= D" " e= E" " f= F" " g= G" " h= H" " i= I" " j= J" " k= K" " l= L" " m= M" " n= N" " o= O" " p= P" " q= Q" " r= R" " s= S" " t= T" " u= U" " v= V" " w= W" " x= X" " y= Y" " z= Z" " = ") Do Call Set "_FileName=%%_FileName:%%~I%%"
:: Last check for multiple spaces
Call :RmLoop
Set _FileName=!_FileName: = !
:: Remove any trailing underscore
Set _FileName=!_FileName:_%%~xA=%%~xA!
Ren "%%A" "!_FileName:~1!%%~xA"
)
PopD
Goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::
:RmLoop
Call Set "_FileName=%%_FileName: = %%"
Echo !_FileName!|Findstr /C:" ">Nul
If !ErrorLevel!==0 Goto RmLoop
What this should do is remove certain characters from filenames and title case the files ie
some_filename = Some Filename
filename number 2 = Filename Number 2
Most of it works fine such as case changing and removing extra spaces and underscores
But i am having trouble with special characters such as !
(10 lines down in code)
if a filename is named ie file! 2 it is ignored
tryed using "^!="
^"!="
but to no avail
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您展开 FOR 变量(如 %%A)时,如果该变量包含
!
或^
,则无法启用延迟展开。延迟扩展会破坏该值,因为单个!
被剥离,两个!
会将其间的字符解释为变量并将其扩展(可能为零),并且^
将被剥离,因为它用于转义!
。最简单的解决方案是将顶部更改为 SETLOCAL DisableDelayedExpansion,并将所有延迟扩展替换为您已在其他地方使用的 CALL 命令%%VAR%% 技巧。 CALL 技巧比延迟扩展慢得多,因此通常我会建议切换延迟扩展。但我认为在你的情况下切换可能会变得复杂。
我还建议您更改 :RmLoop 定义
另外,我不明白为什么在调用 :RmLoop 后将空格替换为自身?
我不确定
**"!="**
替换正在尝试什么。也许可以删除它?You cannot have Delayed Expansion enabled when you expand a FOR variable like %%A if the variable contains
!
or^
. Delayed expansion will corrupt the value because a single!
is stripped, two!
will interpret the chars in between as a variable and expand it (probably to nothing), and^
will be stripped because it is used to escape!
.Your easiest solution is change the top to
SETLOCAL DisableDelayedExpansion
, and replace all delayed expansion with theCALL command %%VAR%%
trick that you are already using elsewhere. The CALL trick is much slower than delayed expansion, so normally I would recommend toggling delayed expansion. But I think the toggling could get complicated in your case.I also suggest you change your :RmLoop definition
Also I don't understand why you replace a space with itself after you call :RmLoop?
I'm not sure what the
**"!="**
replacement is attempting. Perhaps that can be removed?谢谢
设法对其进行排序,将其添加到文件顶部
ps
"!="
只是为了标记出错的地方。Thanks
Managed to sort it add this to the top of the file
ps the
"!="
was just to mark where it was going wrong.