批处理文件,$:和〜

Bil*_*eal 1 batch-file environment-variables

我一直在编写一个需要在文件中扩展环境字符串的应用程序.

为此,我可以使用标准的Windows API函数,ExpandEnvironmentStrings:http://msdn.microsoft.com/en-us/library/ms724265(VS.85) .aspx

我确实对这个功能有一些问题.第一: The size of the lpSrc and lpDst buffers is limited to 32K.

下一个: Note that this function does not support all the features that Cmd.exe supports. For example, it does not support %variableName:str1=str2% or %variableName:~offset,length%.

我想实现cmd.exe允许的这些额外功能,但我不确定它们到底是什么.:〜偏移,长度有点明显......子串.但不确定第一个是什么.

有任何想法吗?

Billy3

pax*_*blo 5

这是字符串替换.

基本上,如果将variableName设置为"I am three",则"%variableName:three=four%"生成"I am four"(为了更好的格式化而放入双引号,它们不构成字符串的一部分).

C:\Documents and Settings\Administrator>set x=I am three

C:\Documents and Settings\Administrator>echo %x%
I am three

C:\Documents and Settings\Administrator>echo %x:three=four%
I am four
Run Code Online (Sandbox Code Playgroud)

您也可以用空字符串替换(显而易见)并从字符串的开头替换(不那么明显):

C:\Documents and Settings\Administrator>echo %x:three=%
I am 

C:\Documents and Settings\Administrator>echo %x:*am=I am not%
I am not three
Run Code Online (Sandbox Code Playgroud)

另外,子字符串变体是Pythonesque,因为负数从字符串的末尾起作用:

C:\Documents and Settings\Administrator>echo %x:~,4%
I am

C:\Documents and Settings\Administrator>echo %x:~-5%
three
Run Code Online (Sandbox Code Playgroud)