2012-02-02 73 views
1
@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但無濟於事

回答

2

當展開像%%的變量,如果變量包含!^你不能有延遲擴展啓用。延遲擴展會破壞該值,因爲一個!被剝離,兩個!將解釋變量之間的字符並將其展開(可能爲空),並且^將被剝離,因爲它用於轉義!

您最簡單的解決方案是將頂部更改爲SETLOCAL DisableDelayedExpansion,並用您在其他地方已經使用的CALL command %%VAR%%替代所有延遲擴展。 CALL技巧比延遲擴展慢得多,所以通常我會推薦切換延遲擴展。但我認爲在你的情況下,切換可能會變得複雜。

我也建議你改變:RmLoop定義

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

此外,我不明白你爲什麼打電話後自身替換空間:RmLoop?

我不確定**"!="**更換嘗​​試的是什麼。也許這可以被刪除?

+0

也許''=''''周圍的'**'只是試圖突出顯示OP有問題的特定部分。 – 2012-02-02 16:21:00

0

感謝 管理對它進行排序它添加到文件的頂部

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