-1

Okay so here is what I have.

@echo off
setLocal EnableDelayedExpansion
:begin
set /a M=0
set /a number=0
set /p Input=You: 
echo %Input% >> UIS
for /F "tokens=1 delims= " %%i in ("%Input%") do (
    set /a M+=1
    set i!M!=%%i
)
del UIS 1>nul 2>nul
:loop
set /a number+=1
set invar=!i%number%!
echo %invar%
pause > nul
goto loop

Say, for example, the Input string was "Lol this is my input string" I want the for loop to set i!M! where M = 1 to "Lol", where M = 2 i!M! is "this" and where M = 3 i!M! is "is" and so on. Now, of course, this can't go on forever, so even if I have to stop when M = 25 or something, and say the string was only 23 words long. Then when M = 24 and 25 then i!M! is simply null or undefined.

Any help is appreciated, thank you.

2 Answers2

1

for /f reads line by line, not word by word.

Here's an answer proposed at How to split a string in a Windows batch file? and modified for your situation:

@echo off
setlocal ENABLEDELAYEDEXPANSION

REM Set a string with an arbitrary number of substrings separated by semi colons
set teststring=Lol this is my input string
set M=0

REM Do something with each substring
:stringLOOP
    REM Stop when the string is empty
    if "!teststring!" EQU "" goto displayloop

    for /f "delims= " %%a in ("!teststring!") do set substring=%%a

    set /a M+=1
    set i!M!=!substring!

    REM Now strip off the leading substring
    :striploop
        set stripchar=!teststring:~0,1!
        set teststring=!teststring:~1!

        if "!teststring!" EQU "" goto stringloop

        if "!stripchar!" NEQ " " goto striploop

        goto stringloop

:displayloop
set /a number+=1
set invar=!i%number%!
echo %invar%
pause > nul
goto displayloop

endlocal
Community
  • 1
  • 1
Nate Hekman
  • 6,507
  • 27
  • 30
0

for /F command divide a line in a definite number of tokens that must be processed at once via different replaceable parameters (%%i, %%j, etc). Plain for command divide a line in an undefined number of words (separated by space, comma, semicolon or equal-sign) that are processed one by one in an iterative loop. This way, you just need to change this for:

for /F "tokens=1 delims= " %%i in ("%Input%") do (

by this one:

for %%i in (%Input%) do (

PS - I suggest you to write the array in the standard form, enclosing the subscript in square brackets; it is clearer this way:

set i[!M!]=%%i

or

set invar=!i[%number%]!
Aacini
  • 65,180
  • 12
  • 72
  • 108