I'm trying to loop through every character in a string . I only however know how to loop for every word in a string ussing the following:
(set /P MYTEXT=)<C:\MYTEXTFILE.txt
set MYEXAMPLE=%MYTEXT%
for %%x in (%MYEXAMPLE%) do (
ECHO DO SOMTHING
)
How can I configure it to work per character rather then per word?
AFAIK, FOR
cannot do a character-wise iteration - A possible workaround is to build a loop like this:
@ECHO OFF
:: string terminator: chose something that won't show up in the input file
SET strterm=___ENDOFSTRING___
:: read first line of input file
SET /P mytext=<C:\MYTEXTFILE.txt
:: add string terminator to input
SET tmp=%mytext%%strterm%
:loop
:: get first character from input
SET char=%tmp:~0,1%
:: remove first character from input
SET tmp=%tmp:~1%
:: do something with %char%, e.g. simply print it out
ECHO char: %char%
:: repeat until only the string terminator is left
IF NOT "%tmp%" == "%strterm%" GOTO loop
Note: The question title states that you want to loop over "every character in variable string", which suggests the input file only contains a single line, because the command (set /P MYTEXT=)<C:\MYTEXTFILE.txt
will only read the first line of C:\MYTEXTFILE.txt
. If you want to loop over all lines in a file instead, the solution is a bit more complicated and I suggest you open another question for that.