views:

89

answers:

1

Hello all :)

I've been writing an application which will need to expand environment strings in a file.

To that effect, I could use the standard windows API function, ExpandEnvironmentStrings: http://msdn.microsoft.com/en-us/library/ms724265(VS.85).aspx

I do have a few problems with that function though. First: The size of the lpSrc and lpDst buffers is limited to 32K.

Next: 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%.

I would like to implement these extras cmd.exe allows, but I'm not sure exactly what they are. :~offset,length is a bit odvious ... substring. But not sure what the first one is.

Any ideas?

Billy3

+4  A: 

It's string substitution.

Basically, if variableName is set to "I am three", then "%variableName:three=four%" generates "I am four" (double quotes put in for nicer formatting, they do not form part of the strings).

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

You can also replace with an empty string (obvious) and replace from the start of the string (not so obvious):

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

In addition, the substring variant is Pythonesque in that negative numbers work from the end of the string:

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

C:\Documents and Settings\Administrator>echo %x:~-5%
three
paxdiablo
I.e., search and replace?
Billy ONeal
Ah, I see now :) Thanks!
Billy ONeal
As a survivor of COMMAND.COM since MSDOS 2 or so, CMD.EXE is so much more pleasant. It is particularly nice to be able to test and demonstrate things like variable substitution directly from an interactive command prompt. In the good old days, certain things like environment variable substitution only worked in batch files, and not at the prompt.
RBerteig