@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
這是什麼應該做的是從文件名和標題的情況下刪除某些字符的文件,即DOS批處理文件的首字母大寫,並刪除特殊字符
some_filename = Some Filename
filename number 2 = Filename Number 2
它的大部分工作正常,如情況發生變化,刪除多餘的空格和下劃線
但我有特殊字符,如!
(代碼中的10行) 如果文件名被命名爲fil電子!二是忽略
使用^"!="
tryed但無濟於事
也許''=''''周圍的'**'只是試圖突出顯示OP有問題的特定部分。 – 2012-02-02 16:21:00