2012-04-03 60 views
4
setlocal enabledelayedexpansion 
for /f "delims=" %%a in (%TempFile2%) do (
    set a=%%a 
    set a=!a: =! 
    echo !a! >>%DataFile% 
) 

我知道代碼查找tempfile2數據中的每個「空白空間」並設置它。這條線是什麼意思?這個批處理命令是什麼意思?

set a =!a:=!

感謝

回答

6

這是一個基本的模式匹配和替換。

這裏是幫助從set /?

Environment variable substitution has been enhanced as follows: 

    %PATH:str1=str2% 

would expand the PATH environment variable, substituting each occurrence 
of "str1" in the expanded result with "str2". "str2" can be the empty 
string to effectively delete all occurrences of "str1" from the expanded 
output. "str1" can begin with an asterisk, in which case it will match 
everything from the beginning of the expanded output to the first 
occurrence of the remaining portion of str1. 

在你的榜樣額外的轉折是,在「延遲擴展」的語法正在被使用,它使用!字符作爲環境變量擴展字符而不是%

因此,命令set a=!a: =!將從變量a的內容中刪除所有空格字符。

由於cmd.exe通常在parens所包含的塊中擴展(然後使用%分隔符),所以需要延遲擴展(或者至少使這樣的事情更容易一些)一部分。

+0

1相同的用於提延遲擴展,其讀取良好。 – cctan 2012-04-04 01:06:48

4

這是一個替換聲明,它用代替所有空格

的語法(可在SET /?找到)
!variableName:findText=replaceText! 或百分比膨脹
%variableName:findText=replaceText%

相關問題