1.

Solve : trouble with a for loop?

Answer»

Ok, I'm using for loops now. So, here's what I'm doing

Code: [Select]for /f %%c in ('ver') do set the_ver=%%c
echo %the_ver%

Which outputs

Code: [Select]microsoft

Because the value set to THE_VER has spaces. Is it possible to get around this? Or is that
just the way it is is.You need to learn about delimiters ("delims"). The default token delimiter when parsing a line of text is a space or spaces. You can override this by including a delims block in your FOR command.

"delims=" SETS the delimiters to be the start and end of the line which is what you want here.

for /f "delims=" %%c in ('ver') do set the_ver=%%c

There is another way of doing this which involves tokens, but we won't bother about that just now.
You can do FOR /? for the details.

Basically, the default is to break up the output into PARAMETERS on each space or tab. In your example, %%c would be the first parameter, %%d would be the 2nd, %%e the 3rd, etc. This allows more detailed extraction of information for batch scripts. If you want the WHOLE string in one variable, the 2 easiest ways would be to set "delims=" or to set "tokens=*".

So try this:
Code: [Select]for /f "tokens=*" %%c in ('ver') do set the_ver=%%c
echo %the_ver%Sorry for posting behind you, Dias ... there was no reply when I started my reply which took about 20 minutes between dealing with a wild kid and 2 noisy dogs.No PROBLEM, 2 heads are better than one. Thank you . I learned something and I fixed my problem.



Discussion

No Comment Found