Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can get it to read the text file until it becomes a comma delimited text file.

I would like to read the two variables on each line and test each one with a If statement for another condition. Problem is I can't read the variables properly. Tried many things but here is what I will post. Timeouts are to see what's happening:

for /f "tokens=*" %%a in (TestText.txt) do (
timeout /t 1
echo %%a is the present variabe
timeout /t 2
if %%a=="One","1" echo Match for "One","1"
timeout /t 3
if %%a=="One""1" echo Match for "One","1"
timeout /t 4
if %%a=="One" echo Match for "One"
timeout /t 5
if %%a=="1" echo Match for "1"
timeout /t 6
)

TestText.txt

"One","1"
"Two","2"
"Three","3"
"Four","4"

OUTPUT:

"One","1" is the present variabe
share|improve this question
    
Unless you say otherwise, I will assume you mean the Windows command line and not a variant of DOS. There is a difference. – Bob Jun 29 '12 at 17:22
1  
@Sms: If you want to edit your question or post a comment, and you already lost your cookie, try registering your account. – Dennis Jun 29 '12 at 17:54

The problem with your condition is that you have to escape the comma (,). On the command prompt, this is done with a caret (^).

This works:

for /f %%a in (TestText.txt) do (
    if %%a=="One"^,"1" echo Match for "One","1"
)

However, since you are using CSV, you might want to split each line by the commas. This is done with the tokens=1-n delims=, option, where n is the number of values per line.

The separate values will be stored in %%a, %%b, etc.:

for /f "tokens=1-2 delims=," %%a in (TestText.txt) do (
    if %%a=="One" echo Match for "One"
    if %%b=="1"   echo Match for "1"
)

Further information

share|improve this answer
2  
Note that specifying tokens=1* will actually store every token after the first one in %%b. If the input line were "One","1","a" then %%a would get "One" and %%b would get "1","a". Arguably, the more correct way is tokens=1,2, which will ignore any tokens after the second one. Of course, the choice of which to use depends on the situation. – Bob Jun 29 '12 at 17:38
    
Oh, and good answer. I'm too slow :P – Bob Jun 29 '12 at 17:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .