Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping for every character in variable string BATCH

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?

like image 826
LabRat Avatar asked Feb 21 '13 14:02

LabRat


People also ask

What is %% f in batch file?

%%parameter A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a text file consists of reading the file, one line of text at a time and then breaking the line up into individual items of data called 'tokens'.

What is %% K in batch file?

So %%k refers to the value of the 3rd token, which is what is returned.

How do I iterate through a batch file?

In Batch Script, the variable declaration is done with the %% at the beginning of the variable name. The IN list contains of 3 values. The lowerlimit, the increment, and the upperlimit. So, the loop would start with the lowerlimit and move to the upperlimit value, iterating each time by the Increment value.

How do I loop a batch script only a certain amount of times?

There is an example: @echo off set loop=0 :loop echo hello world set /a loop=%loop%+1 if "%loop%"=="2" goto next goto loop :next echo This text will appear after repeating "hello world" for 2 times. Output: hello world hello world This text will appear after repeating "hello world" for 2 times.


2 Answers

This is a simple and direct way to loop through every character in a string:

@echo off
setlocal ENABLEDELAYEDEXPANSION

set /P mytext= < MYTEXTFILE.txt
echo Line is '%mytext%'

set pos=0
:NextChar
    echo Char %pos% is '!mytext:~%pos%,1!'
    set /a pos=pos+1
    if "!mytext:~%pos%,1!" NEQ "" goto NextChar
like image 153
jimhark Avatar answered Nov 03 '22 00:11

jimhark


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.

like image 33
zb226 Avatar answered Nov 03 '22 02:11

zb226