Dos批处理文件标题大写并删除特殊字符

发布于 2025-01-01 23:10:46 字数 1830 浏览 2 评论 0原文

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小红帽 2025-01-08 23:10:46

当您展开 FOR 变量(如 %%A)时,如果该变量包含 !^,则无法启用延迟展开。延迟扩展会破坏该值,因为单个 ! 被剥离,两个 ! 会将其间的字符解释为变量并将其扩展(可能为零),并且 ^ 将被剥离,因为它用于转义 !

最简单的解决方案是将顶部更改为 SETLOCAL DisableDelayedExpansion,并将所有延迟扩展替换为您已在其他地方使用的 CALL 命令%%VAR%% 技巧。 CALL 技巧比延迟扩展慢得多,因此通常我会建议切换延迟扩展。但我认为在你的情况下切换可能会变得复杂。

我还建议您更改 :RmLoop 定义

:RmLoop
Call Set "_FileName2=%%_FileName:  = %%"
If "%_FileName2%" neq "%_FileName%" (
  set "_FileName=%_FileName2%"
  goto :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 the CALL 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

:RmLoop
Call Set "_FileName2=%%_FileName:  = %%"
If "%_FileName2%" neq "%_FileName%" (
  set "_FileName=%_FileName2%"
  goto :RmLoop
)

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?

仙气飘飘 2025-01-08 23:10:46

谢谢
设法对其进行排序,将其添加到文件顶部

ps "!=" 只是为了标记出错的地方。

@Echo Off

SetLocal DisableDelayedExpansion
  For /F "Tokens=*" %%J In ('Dir /B *.txt^|Findstr "!"') Do Call :Renamee "%%J"

..

..

..

..

..

::This at the very bottom

:Renamee

Set _FileTemp=%~1

Set _FileTemp=%_FileTemp:!=%

Ren "%~1" "%_FileTemp%"

Thanks
Managed to sort it add this to the top of the file

ps the "!=" was just to mark where it was going wrong.

@Echo Off

SetLocal DisableDelayedExpansion
  For /F "Tokens=*" %%J In ('Dir /B *.txt^|Findstr "!"') Do Call :Renamee "%%J"

..

..

..

..

..

::This at the very bottom

:Renamee

Set _FileTemp=%~1

Set _FileTemp=%_FileTemp:!=%

Ren "%~1" "%_FileTemp%"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文