Why does a list with & without spaces print differently?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rojouz
Posts: 2
Joined: 17 Jan 2015 19:07

Why does a list with & without spaces print differently?

#1 Post by rojouz » 17 Jan 2015 19:23

I am looking at the following example listed here: http://stackoverflow.com/a/17606350/2519402

(I don't have enough points to ask my question there via a comment).

Now, for the first code example mentioned there of:

Code: Select all

set list=A B C D


and the slightly modified code of:

Code: Select all

(for %%a in (%list%) do (
   echo %%a
))


That prints A B C D each on a line by itself, so, like this:

Code: Select all

A
B
C
D


If the spaces are removed in the list, so like this:

Code: Select all

set list=ABCD


each letter is not read, and the list prints only this:

Code: Select all

ABCD


1. Why is that happening?
2. How can I get list to be printed on a line of its own when it is created with no spaces?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Why does a list with & without spaces print differently?

#2 Post by Squashman » 17 Jan 2015 23:37

Windows Batch files use certain characters as default delimiters. I can't remember what they all are. But too preserve the spaces you would do just what you would do when there are spaces in your file names. You put it in double QUOTES.

Code: Select all

@echo off

echo space delimiter
FOR %%G in (A B C D) DO echo %%G
echo comma delimiter
FOR %%G in (A,B,C,D) DO echo %%G
echo tab delimiter
FOR %%G in (A   B   C   D) DO echo %%G
echo semicolon
FOR %%G in (A;B;C;D) DO echo %%G
echo using quotes to preserve the spaces.
FOR %%G in ("A B C D") DO echo %%G
pause

output

Code: Select all

space delimiter
A
B
C
D
comma delimiter
A
B
C
D
tab delimiter
A
B
C
D
semicolon
A
B
C
D
using quotes to preserve the spaces.
"A B C D"
Press any key to continue . . .

rojouz
Posts: 2
Joined: 17 Jan 2015 19:07

Re: Why does a list with & without spaces print differently?

#3 Post by rojouz » 19 Jan 2015 22:56

To Squashman: Thanks for the response.

If a delimeter causes items in a list to be printed separately, and also apparently for each item to be processed separately, why does the following code work:

Code: Select all

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "text=This is my zero text example"
SET "alfa=0abcdefghijklmnopqrstuvwxyz"
FOR /l %%x IN (1,1,26) DO SET "$!alfa:~%%x,1!=%%x"
SET /a count=0
:loop
SET "char=!text:~%count%,1!"
SET "code=!$%char%!
SET /a count+=1
IF DEFINED char SET "line=!line!%code% "&GOTO :loop
ECHO %text%
ECHO %line%


which came from here: http://stackoverflow.com/a/18378208/2519402

I am trying to create the opposite of this code in which numbers are converted to letters, and would like to solve it myself by understanding how items in a list are processed.

Also, if it's not too much trouble, can you please explain this line:

Code: Select all

DO SET "$!alfa:~%%x,1!=%%x
(especially the $ symbol, the tilde (~), and the ,1)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Why does a list with & without spaces print differently?

#4 Post by foxidrive » 20 Jan 2015 03:27

rojouz wrote:and would like to solve it myself by understanding how items in a list are processed.
Also, if it's not too much trouble, can you please explain this line:

Code: Select all

DO SET "$!alfa:~%%x,1!=%%x
(especially the $ symbol, the tilde (~), and the ,1)

It's refreshing to see someone that it trying to understand the code.

After the line of code that you took the above from, add a line that says

Code: Select all

set $ & pause

which should give you a clue, and you will find the various uses of the set command from the help, by typing set /?

Post Reply