3

I have a desire to read lines from a TXT file into an Array structure for use in a batch file I am using (to read in configuration elements currently hardcoded).

A few notes/assumptions:

  1. .TXT file in same directory as the .BAT file
  2. Only 2 columns to parse, unknown number of rows
  3. Col1 & Col2 data can contain spaces, but no special chars
  4. Format/delimiter of the .TXT file can be whatever is convenient to this task: Ex: Col1 | Col2

I'm just looking for a few pointers to get me started.

Thanks!

Mark

12
  • Note, while there are ways to emulate arrays, batch does not support arrays. Commented May 22, 2015 at 18:09
  • What @aphoria said. You need to clarify what you mean by "into an Array structure", as batch does not have any built-in concept of "array". Commented May 22, 2015 at 18:13
  • 2
    @MarkPelletier: See this post. @ others: Suppose this command: set /A result=2+3. As you know, batch does not support numeric variables nor have any built-in concept of "number", so the number 5 stored in result variable, is an "emulated" number or a "simulated" one? Because accordingly to your point of view, it is NOT a "real" number five! Commented May 22, 2015 at 22:37
  • 2
    @rojo: I only want to note that when someone asks for "arithmetic operations in batch", for example, nobody answer: "batch does not support numbers nor arithmetic operations, while there are ways to emulate they like /A switch of set command", but many people insist to clear the point about arrays. Why? Note also that all operations that can be achieved in a programming language that "support arrays" can also be achieved in Batch (in one or other way). Bottom line: I don't see the usefulness of the frequent clarification about "batch does not support arrays". Commented May 22, 2015 at 23:20
  • 3
    Agreed. Neither do I. It's a pedantic argument that offers no solution to the problems askers are trying to solve. I think I understand your irritation. I feel the same frustration from people who comment, "You should use Powershell", but never actually offer an answer explaining why the OP should use Powershell. No, Bill_Stewart, Powershell is not the Messiah. It's not always the answer no matter the question. Commented May 22, 2015 at 23:33

1 Answer 1

5

Simulation of a 2-dimentional numerically-indexed array:

Contents of textfile.txt:

var 1,val 1
var 2,val 2
var 3,val 3

Contents of test.bat:

@echo off
setlocal enabledelayedexpansion

set idx=0

for /f "usebackq tokens=1* delims=," %%I in ("textfile.txt") do (
    set "var[!idx!][0]=%%~I"
    set "var[!idx!][1]=%%~J"
    set /a idx += 1
)

set var

Resulting output:

var[0][0]=var 1
var[0][1]=val 1
var[1][0]=var 2
var[1][1]=val 2
var[2][0]=var 3
var[2][1]=val 3

Or you could simulate associative arrays, whose key-value pair format might make more sense if you're dealing with configuration data.

Simulation of an associative array:

Contents of textfile.txt:

key 1=val 1
key 2=val 2
key 3=val 3

Contents of test.bat:

@echo off
setlocal

for /f "usebackq tokens=1* delims==" %%I in ("textfile.txt") do (
    set "config[%%~I]=%%~J"
)

set config

Resulting output:

config[key 1]=val 1
config[key 2]=val 2
config[key 3]=val 3
Sign up to request clarification or add additional context in comments.

1 Comment

Rojo, that's a fantastic start! Both variations of your simulation could apply to my needs. I'll tinker a bit to see which provides the best fit. I'll report back any findings or adjustments. Thanks!. - Mark

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.