Trying to create a batch file that creates a string of numbers based on the number of words in a sentence.
I have a variable that contains a varying number of words, eg: sentence="this is a sentence"
The string I need to generate would be "1 2 3 4" as there is 4 words in the sentence.
Similarly, sentence="this is a longer sentence because reasons" would generate "1 2 3 4 5 6 7"
I'm trying something along the lines of this:
SET sentence=this is a longer sentence because reasons
SET count=
SET numbers=1
FOR %%a IN (%sentence%) DO (
SET "numbers=%numbers% %count%" & SET /A count+=1
)
ECHO Resulting number string: %numbers%
ECHO Counter: %count%
... to keep appending the increasing count variable to the end of number. So each time the FOR loop runs, it goes "1 2", "1 2 3", "1 2 3 4", etc.
The counter is working correctly, reporting "Counter: 7" But the string just reports "Resulting number string: 1 "
It's not adding the counter to the end... and when I have had it append, it results in "1 7" instead of "1 2 3 4 5 6 7"
This is regardless of whether I use setlocal EnableDelayedExpansion or not.
What am I doing wrong here?
(EDIT: This question relates to APPENDING an incrementing number to the end of a string. As I mentioned in my original question, EnableDelayedExpansion make no difference if enabled or disabled)