2009-04-28 117 views
1

我一直在編寫一個應用程序,它需要在文件中擴展環境字符串。

爲此,我可以使用標準的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

回答

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 

您也可以使用空字符串(明顯)取代,並從字符串的開始(不那麼明顯)取代:

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 

此外,子變體是Pythonesque在負

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

C:\Documents and Settings\Administrator>echo %x:~-5% 
three 
+0

也就是說,搜索並替換:數字從結束的字符串的工作? – 2009-04-28 01:37:29