You are not logged in.

#1 26 Apr 2016 13:17

jkruijt
New Member
Registered: 26 Apr 2016
Posts: 2

Using FOR /F with variable amount of tokens

Hi There,

I try to use FOR /F in a kind of generic way. The _tokens parameter can have several different arguments, such as '2' or '3,5' or any other combination up until the highest value of 6 (in the example below). When all the fields are use and _tokens has the argument '1,2,3,4,5,6' (without the quotes), the process will display 6 values in the :show sub process.
But when _tokens only has the value '2,3' or so, the outcome is not okay as you can see in the last code block.

The first line of the text file is ignored and the second line is as follows:

xxxxxxxxx,aajp,7203,7203.T,xxxx,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",-4.4932,-31.1746


BATCH FILE

SET _tokens=2,3
SET _sep=,

FOR /F "skip=1 tokens=%_tokens% delims=%_sep%" %%A in ('TYPE filename') do CALL :s_show "%%A","%%B","%%C","%%D","%%E","%%F"
goto :end
  
  :s_show
  echo 1=%~1
  echo 2=%~2
  echo 3=%~3
  echo 4=%~4
  echo 5=%~5
  :s_show_exit
  pause
  goto :eof

:end 

When PAUSE is reached the script displays:

DISPLAY

1=aajp
2=7203
3=D
4=F
5=
Press any key to continue . . .

In the next step I need to read those vars into a array. When a var is empty, there will be not load. In this case now, the var that should be empty isn't and is holding the var character.
Note: var character C and E are ignored for reason unknown.

I hope somebody has any thoughts concerning this issue. I would appreciate any help!

regards....

Offline

#2 03 May 2016 13:40

elias
Member
Registered: 03 May 2016
Posts: 12

Re: Using FOR /F with variable amount of tokens

Hi,

It is not clear what you are asking for 100%.

Can you please post a sample line where only tokens 2 and 3 are present as you described?


Book: Batchography - The Art of Batch Files Programming

Offline

#3 03 May 2016 14:49

jkruijt
New Member
Registered: 26 Apr 2016
Posts: 2

Re: Using FOR /F with variable amount of tokens

Hi elias,

The whole point is that the FOR command is part of a batch job, which is called by another batch job and passing the token variable to the batch job with the FOR command.
In the example I have as input a text file with 8 columns separated by a comma.
I call the "FOR batch" with the variable for the tokens option:

CALL FOR-batch.bat "2,3" 

The "for batch" has a command line that is executing the FOR command:

FOR /F "skip=1 tokens=%_tokens% delims=%_sep%" %%A in ('TYPE filename') do CALL :s_show "%%A","%%B","%%C","%%D","%%E","%%F"

Note: for the example I only use 6 %% parameters although the input file has 8 fields.

The "show subroutine" displays the %% parameters in a for me unpredictable manor. It does at follows:

1=aajp
2=7203
3=D
4=F
5=
Press any key to continue . . . 

NOTE: again, I show only 5 rows, but for the example it is enough.

I do non understand why %3 shows the value D (instead of being empty or...showing C)
The same goes for %4 showing the value F. Because tokens is 2,3 in the FOR command, I expected only %1 and %2 would carry a value and the rest of the parameters would be empty.

Hope this clarifies my question.

Regards,
John

Offline

#4 03 May 2016 16:17

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: Using FOR /F with variable amount of tokens

This is totally crazy, but I figured it out.

At runtime, the call gets expanded to

call :s_show "aajp" "7203" "%C" "%D" "%E" "%F"

The interpreter is trying to convert the pairs of percent signs it finds into other variables! It's treating the line as follows:

call :s_show
%1 = "aajp"
%2 = "7203"
%3 = %C" "%D (note that it's reading this as the variable C" " surrounded by percent symbols, followed by a plain D, which is why %3 is showing up as D)
%4 = %E" "%F (same as above, but with F)

The best way to get around this is to not use the for tokens that you don't need.

Offline

#5 03 May 2016 17:50

elias
Member
Registered: 03 May 2016
Posts: 12

Re: Using FOR /F with variable amount of tokens

Shadow Thief, good catch! smile


Book: Batchography - The Art of Batch Files Programming

Offline

Board footer

Powered by