2012-04-03 85 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%