|
Answer» can i have a for loop so every character is assigned a diffrent %%X for ex 123456 becomes %%a=1 %%B=2 %%c=3 %%d=4 %%a=e %%f=6No you can't. The FOR command cannot separate a STRING into characters. If you let US know what you are TRYING to do somebody might be able to suggest a method, or were you just curious? just curiousYou have to give the FOR command one or more "delimiters", to use when deciding when one token ends and the next one begins. Delimiters can bethe line start and end, one or more characters.
You could have "delims=." and then for could read a.b.c.d.e etc and separate them out into tokens. You can insert the delimiters with one for statement and then parse the new string with another for statement. The question is why? Tokens as opposed to variables only live as long as the for loop. Seems like a lot of work for so little payoff.
Code: [Select]echo off
setlocal enabledelayedexpansion set str=123456
for /l %%v in (0,1,5) do ( set string=!string!!str:~%%v,1!- ) for /f "tokens=1-6 delims=-" %%a in ("!string!") do ( echo %%a %%b %%c %%d %%e %%f )
I used a DASH (-) as the delimiter, but it is arbitrary. Just don't use any character that has special meaning in batch language.
Good luck. You would need to know the string length in advance, and I am not sure what advantage putting the characters into loop metavariables would have over the ordinary sltring slicing methods. In essence the code above takes about 6 lines to slice the string into 6 characters just so you can... slice it into 6 characters.
Quote from: mat123 on June 26, 2010, 08:32:56 AM can i have a for loop so every character is assigned a diffrent %%X for ex 123456 becomes %%a=1 %%b=2 %%c=3 %%d=4 %%a=e %%f=6
as i have always suggested, if you want to do various programming tasks easily and productively, such as this one you mentioned, use a good programming language.
Example in Python
Code: [Select]# python Python 2.6.2 (r262:71600, Aug 21 2009) Type "help", "copyright", "credits" or "license" for more information. >>> my_string="abcdefg" >>> list(my_string) ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>>
Now you have all your individual characters separated. Easy isn't it?
|