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

Mark Pelletier
  • 1,329
  • 2
  • 24
  • 42
  • Note, while there are ways to emulate arrays, batch does not support arrays. – aphoria May 22 '15 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". – Ryan Bemrose May 22 '15 at 18:13
  • Thank you both. My preliminary poking around seemed to imply and demonstrate array usage within batch files. They must have been "emulations" as you note. - Mark – Mark Pelletier May 22 '15 at 21:35
  • 2
    @MarkPelletier: See [this post](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). @ 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! – Aacini May 22 '15 at 22:37
  • @Aacini I think the general implication is that arrays in other languages are objects, rather than merely collections of similarly-named variables. As objects, true arrays have prototypical methods and properties -- `array.sort()`, `array.splice()`, `array.apply()`, `array.length`, and so on. Batch simply doesn't offer the same behavior unless you code it yourself. Between a batch array and a collection of batch variables, there is no functionality that distinguishes one from the other. Not to step on toes, though. Your observation is not wrong. – rojo May 22 '15 at 22:46
  • 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". – Aacini May 22 '15 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. – rojo May 22 '15 at 23:33
  • @rojo: +1 for your last comment! **`:)`** – Aacini May 22 '15 at 23:59
  • @Aacini I don't think it's pedantic to point out that batch does not truly have arrays. Showing less experienced batch programmers how to emulate arrays in batch is great, but including the caveat that they are not really arrays is appropriate. Your comparison to numbers is invalid because batch does indeed support numeric variables and arithmetic operations. I don't think most people would accept any other language claiming to offer arrays, when it actually required consecutively numbered variables of the same name. – aphoria May 29 '15 at 17:05
  • @aphoria: The problem with these discussions is that are based on _opinions_, not facts. After `set /A num=2+3` command, the `5` stored in `num` is not a 16/32-bits integer nor a single BCD digit, so _in my opinion_ is NOT a number! What are the facts in this case? Most Microsoft sites specify: "Environment variables are strings that...", and the [Win-32 API reference](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682009.aspx) is more precise: "The environment block consists of ... null-terminated strings, where each string is in the form: `name=value`". A string is NOT a number! – Aacini May 30 '15 at 03:29
  • @Aacini Yes, they are stored as strings, but you can perform math operations on them, so they act as numbers. I realize the difference between this and batch "arrays" not being real is small, but IMO it is enough to warrant telling people who don't know. I'm not trying to start a war with you...I just feel it's good for people to know. I agree it's not useful to just say batch doesn't support arrays and not offer any solutions. After I left my original comment, rojo left a good answer before I could get back to it. It wasn't my intent to just leave an unhelpful comment. I hope we're cool. – aphoria May 30 '15 at 20:40
  • @aphoria: Yes, there is not a problem at all! **`:)`** I enjoy these discussions when the opponent understand my point of view. I know that this Batch trick can not even be compared vs. the array features of a "real" programming language, but I disagree that this management is not called "real array". If you write `vector[1]=Element one` at hand in a piece of paper, then it _is_ a "real array"! How many array features a language must have in order to call "real" its arrays? (I think this is an absurd question). In my opinion, the _concept_ is more important than the implementation details... – Aacini May 31 '15 at 19:11

1 Answers1

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
rojo
  • 24,000
  • 5
  • 55
  • 101
  • 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 – Mark Pelletier May 22 '15 at 21:33