3

I have a txt file with below data

aaaa 1000 2000
bbb  3000 4000
cccc 5000 
ddd  6000 7000 8000 

The numbers of rows in this file are not fixed.

I need the first token of each row within an array and to print each element.

Mofi
  • 46,139
  • 17
  • 80
  • 143
NewQueries
  • 4,841
  • 11
  • 33
  • 39
  • 1
    Can a Windows/DOS batch file even *do* arrays? I mean, i imagine a PowerShell script could do them, but CMD? – cHao Feb 25 '12 at 22:32
  • @cHao The below links give some hint about arrays in batch file but I am unable to understand it much [http://jakash3.wordpress.com/2009/12/18/arrays-in-batch/] [http://www.robvanderwoude.com/battech_array.php] – NewQueries Feb 26 '12 at 00:02
  • 1
    I don't understand why someone thinks that cHao's comment above is a great comment; it just shows his ignorance about CMD Batch capabilities! ;) – Aacini Feb 26 '12 at 03:40
  • @Aacini: "Capabilities" kinda implies not having to jump through a half dozen hoops to do what can be done in any *respectable* language with one line of code. :) – cHao Feb 27 '12 at 19:32
  • @cHao: Please, read my comment again. I am NOT criticizing your post nor defending Batch. I said that your first comment is a pure and simple negative opinion of Batch (and the second one above is exactly the same) and I just expressed my surprise that anyone can think that *that* is a great comment! Perhaps you may want to explain me why a negative comment deserve a "great comment" qualification, so I may start posting negative comments on PHP (that I don't know)! – Aacini Feb 28 '12 at 04:25
  • @Aacini: It's not a negative opinion or a negative comment. At least, it wasn't as negative as your reply. :) It's a valid question (i wasn't sure whether it could or not, at the time), and probably one that someone else had as well. – cHao Feb 28 '12 at 04:36

3 Answers3

13

To create the array:

setlocal EnableDelayedExpansion

set i=0
for /F %%a in (theFile.txt) do (
   set /A i+=1
   set array[!i!]=%%a
)
set n=%i%

To print array elements:

for /L %%i in (1,1,%n%) do echo !array[%%i]!

If you want to pass the array name and lenght as subroutine parameters, then use this way:

call theSub array %n%

:theSub arrayName arrayLen
for /L %%i in (1,1,%2) do echo !%1[%%i]!
exit /B
Aacini
  • 65,180
  • 12
  • 72
  • 108
2

try this:

@echo off
for /F "tokens=1,2*" %%x in  (myFile.txt) do echo %%x

the double % is required for use in a batch file, but you can test it on the cmd line with single %s.

in a nutshell, the for will iterate over myFile.txt break each line into two tokens using the default delimiter (space).

akf
  • 38,619
  • 8
  • 86
  • 96
  • Thanks @akf, but I need to create an array and pass it into another subroutine where I can fetch each of its elements individually. – NewQueries Feb 25 '12 at 23:53
0

try this and call it from anywhere

@echo off

for /f "usebackq" %%a in ('%2') do set d=%%~a
for /f "usebackq tokens=* delims=%d%" %%G in ('%3')  do set %1=%%~G

set /a i=-1


for %%h in (!%1!) do (
set /a i+=1
set %1[!i!]=%%h
)
Shahim Khlaifat
  • 63
  • 1
  • 1
  • 9
  • 3
    Whilst this may be an answer it seems very similar to the other older answers. When answering an old question that already has an accepted answer you need to add significant extra new information and an explanation of why your new answer is useful. – AdrianHHH Jul 19 '15 at 15:11