2009-04-28 9 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のができますこれらのエキストラを実装したいと思いますが、私は彼らが正確にわからないんだけど。 :〜offset、lengthは少し明白です...部分文字列です。最初のものが何であるかはわかりません。

アイデア?

ビリー3

答えて

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

すなわち、検索と置換:数字は、文字列の末尾から動作しますか? –

+0

ああ、私は今参照してください:)ありがとう! –

+0

MSDOS 2以降のCOMMAND.COMの生存者として、CMD.EXEはもっと楽しいです。対話的なコマンドプロンプトから直接変数置換などのことをテストして実演できるのは特に良いことです。古き良き時代、環境変数の置き換えのようなものは、プロンプトではなく、バッチファイルでのみ機能しました。 – RBerteig