I am trying to use the Windows CMD SET
string manipulation versions in a programmatic way. The two versions of SET
are ...
SET New_String=%String:Old_Text=New_Text%
In variable String
replace Old_Text
with New_Text
, and return result in variable New_String
.
SET New_String=%String:~Number_Of_Chars_To_Skip,Number_Of_Chars_To_Keep%
In variable String
extract Number_Of_Chars_To_Keep
after skipping Number_Of_Chars_To_Skip
, and return result in variable New_String
Old_Text, New_Text, Number_Of_Chars_To_Skip, Number_Of_Chars_To_Keep
must all be literals, not variables. So, typical usage would be like this ...
SET New_String=%String:abcd=fghi%
SET New_String=%String:~2,4%
Usage like this won't work ...
SET New_String=%String:%Old_Text%=%New_Text%%
SET New_String=%String:~%Skip_Count%,%Keep_Count%%
To do the above two SET
s you have to CALL SET
, like ...
CALL SET New_String=%%String:%Old_Text%=%New_Text%%%
CALL SET New_String=%%String:~%Skip_Count%,%Keep_Count%%%
So, I have this following test code fragment ...
SET "Chars=" & SET "String=0123456789" & SET "Skip=1" & SET "Keep=3"
CALL SET Chars=%%String:~%Skip%,%Keep%%%
ECHO Chars="%Chars%" (expected to be "123")
From a CMD file this works correctly. The CALL
has been expanded to SET Chars=%string:~1,3%
and has returned the expected result 123. But, and a big but it is, from a CMD Window (with the same variables) the exact same CALL SET
returns this ...
%0123456789Skip%,3%%
Why doesn't this work in a CMD Window? I researched around and haven't found any info explaining why.