views:

46

answers:

1

I am trying to extract tokens from a list of strings using a batch script, but for some reason it ignores my string if it contains an asterisk.

An example to illustrate this problem is as follows:

@echo off

set mylist="test1a,test1b"
set mylist="test2a,test2b*" %mylist%
set mylist="test3a,test3b" %mylist%

echo %mylist%

for %%a in ( %mylist% ) do (
    for /F "tokens=1,2 delims=," %%i in ( %%a ) do (
        echo %%i 
        echo %%j 
    )
)

I would expect this to print out all six tokens but instead it only prints test3a, test3b, test1a, and test1b, like it is ignoring the second string completely.

The placement of the asterisk within the second string doesn't seem to matter, but if I remove it everything works as I expect.

Does anyone know what is going on here?

+1  A: 

Got it. The interpreter is trying to match a filename. If you change "test2a,test2b*" by pp.* and create a file named pp.txt (same dir) your script will proces the contents of pp.txt

belisarius
Do you have any idea how to get it to stop looking for a file and treat my strings as literals?
bde
Nope. The "for" calls a child CMD.EXE that expands the list. As suggested by @Ben Burnett try to use another special char and replace it later by an asterisk post-processing the output file. Sorry.
belisarius