for /F "tokens=1-11

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rpc86
Posts: 3
Joined: 19 Mar 2015 02:49

for /F "tokens=1-11

#1 Post by rpc86 » 19 Mar 2015 02:58

Hi guys,

I'm new on this forum and glad to have found like this. Got problem on my batch file with 11 parameters.
My text file has 11 columns delimited by comma. When I echo the batch file it does not give me the correct result
especially on the 10th and 11th column. Here is my code:

Code: Select all

for /F "tokens=1-11 delims=," %%a in (new_acct_8.txt) do call :process %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k(
    :process   
   set acct_id=%1
   set acct_name=%2
   set acct_abbr=%3
   set dual_curr=%4
    set att_code1=%5
   set att_code2=%6
   set att_code3=%7
   set att_code4=%8
   set att_code5=%9   
   set att_code6=%10
   set att_code7=%11
   
   rem call another batch file here to pass the above variables
    rem call csi file
   
   rem below line is to view the content of new_acct_8.txt
   
   echo. %acct_id% %acct_name% %acct_abbr% %dual_curr% %att_code1% %att_code2%  %att_code3%  %att_code4%  %att_code5% %att_code6%  %att_code7%  >>result.txt
      
   :end
   
)   
   


Thanks for the help.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: for /F "tokens=1-11

#2 Post by jeb » 19 Mar 2015 07:05

Hi rpc86,

rpc86 wrote:My text file has 11 columns delimited by comma. When I echo the batch file it does not give me the correct result
especially on the 10th and 11th column.

It's hard to guess what's wrong without showing what is your input and what you expect as output.

I guess your input is somewhat like
"col1,col2,,col4..."

So the content of %%a will be col1, %%b = col2
and now you expect that %%c will be empty and %%d is col4 :?:

But you get instead %%c = col4.

The problem here is that FOR/F will reduce multiple contiguous delimiters to one.

Or do you get problems with the call :process %%a %%b :?:
The syntax seems at all a bit confused here, :process shall be a function but is placed at the wrong place and it will fail to call it with empty parameters at all.
I would avoid a call here.

You need to first replace your delimiters so that they can't be empty.

Something lie this should work

Code: Select all

setlocal EnableDelayedExpansion
set "line=col1,col2,,col4..."
set "line="!line:,=","!""
echo !line!
FOR /F "tokens=1-11 delims=," %%a in ("!line!") do (
  set "arg1=%%~a"
  set "arg2=%%~b"
  set "arg3=%%~c"
  echo !arg1!-!arg2!-!arg3!-...
)

rpc86
Posts: 3
Joined: 19 Mar 2015 02:49

Re: for /F "tokens=1-11

#3 Post by rpc86 » 19 Mar 2015 19:24

Hi Jeb,

Thanks for the reply. Below is my text file:

Code: Select all

80000001,SAMPLE ACCOUNT 1,SAMPLE 1,Y,A02,R,CUS3,A1000-0005,HKD,A1000-0010,A1000-0005
80000002,SAMPLE ACCOUNT 2,SAMPLE 2,Y,A03,,,,,2,3
80000003,SAMPLE ACCOUNT 3,SAMPLE 3,N,A04,,,,,4,5
80000004,SAMPLE ACCOUNT 4,SAMPLE 4,Y,A05,R,,,,6,7
80000005,SAMPLE ACCOUNT 5,SAMPLE 5,N,A06,,CUS3,,,8,9



My problem seems to be the limit of token, 9 only. I have 11 parameters to pass. When I echo the
%att_code6% and %att_code7% (10th and 11th), %att_code6% gives me the value of the first column and %att_code7% as the second column.

I noticed the last two variables when I typed them. The 0 of 10 and 1 of 11 are not color orange. Seems it only recognizes upto 9 only.

set att_code6=%10
set att_code7=%11

I used notedpad++

Any workaround ?

Thanks.

rpc86
Posts: 3
Joined: 19 Mar 2015 02:49

Re: for /F "tokens=1-11

#4 Post by rpc86 » 19 Mar 2015 19:35

Hi Jeb,

Your sample is correct. I followed it and it worked.

Thanks.

Post Reply