0

I am very new, I have trough a week of tutorial with batch programing and got somewhere. How ever I still have not figured out this:

my list.txt has lines as

date, name, status, info, comment exp: 2014-01-01, Jenny, OK, blabaa, nocomment

for /f "tokens=* " %%a in (list.txt) do (
    set string=%%a
    call set datearray[i] =%string:~0,10%
    call set namearrat[i]=%string:~12,9%
   call set statusarray[i]=%string:~23,6%
    call set infoarray[i]=%string:~31,3%
  call set comment[i]=%string:~36,4%

)

then later do a forloop to write out the items in another batfile

I need help with 2 things: 1) my code is wrong please help with an real example I am new on this 2) how do I get the value out of the arrays ?

I know there are some small answers in the forum but I can not understand them. Thanks for all help please do explain in real examples so I can understand better.

manam
  • 29
  • 4

1 Answers1

0

If the lines in list.txt file have fields separated by commas, it is easier to get the tokens via a for /f "tokens=1-9 delims=," command instead of the substrings you use:

for /f "tokens=1-9 delims=," %%a in (list.txt) do (
   set datearray[i]=%%a
   set namearray[i]=%%b
   . . .
   set nocomment[i]=%%i
)

The right way to store array elements is using DelayedExpansion and enclose the index variable in exclamation marks instead of percents:

@echo off
setlocal EnableDelayedExpansion

set n=0
for /f "tokens=1-9 delims=," %%a in (list.txt) do (
   set /A n+=1
   set datearray[!n!]=%%a
   set namearray[!n!]=%%b
   . . .
   set nocomment[!n!]=%%i
)

The way to get out the value of elements is via a for /L command to vary the index and enclose the whole element in exclamation marks:

for /L %%i in (1,1,%n%) do (
   echo Date: !datearray[%%i]!, Name: !namearray[%%i]!, etc...
)

Further details at: this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108