Dos batch file titlecase and remove special characters

99封情书 提交于 2019-12-11 08:44:20

问题


@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


回答1:


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?




回答2:


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%"


来源:https://stackoverflow.com/questions/9111723/dos-batch-file-titlecase-and-remove-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!