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.

6
  • 1
    Can a Windows/DOS batch file even do arrays? I mean, i imagine a PowerShell script could do them, but CMD? Commented Feb 25, 2012 at 22:32
  • @cHao The below links give some hint about arrays in batch file but I am unable to understand it much [jakash3.wordpress.com/2009/12/18/arrays-in-batch/] [robvanderwoude.com/battech_array.php] Commented Feb 26, 2012 at 0: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! ;) Commented Feb 26, 2012 at 3: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. :) Commented Feb 27, 2012 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)! Commented Feb 28, 2012 at 4:25

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Aacini I will try this out in my program !
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).

1 Comment

Thanks @akf, but I need to create an array and pass it into another subroutine where I can fetch each of its elements individually.
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
)

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.