0

I was wondering if there's a simple way to loop through all the variables that end with a character in Batch?

For instance, I have the following variables set:

SET id1=something
SET id2=something1
SET id3=something3

then I would like to loop through them using id* something like:

for %%a in ("%id*%") do echo %%a

Using array is not an option for the use case that I have.

any ideas would be extremely appreciated.

Spaniard89
  • 2,359
  • 4
  • 34
  • 55
  • variables ends with character or their values? – npocmaka Jun 06 '17 at 10:52
  • @npocmaka The variables do. – Spaniard89 Jun 06 '17 at 10:53
  • What you have here _is_ an array with the square brackets of the subscripts omitted. That is, in a Batch file `id1` is entirely equivalent to `id[1]`, but the second form is clearer. I invite you to read [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) and also [this one](https://stackoverflow.com/questions/10544646/dir-output-into-bat-array/10569981#10569981) – Aacini Jun 06 '17 at 12:44

1 Answers1

1

The answer you're looking for is:

Set id

If you want to be able to split the result up then you can put it into a for loop:

For /F "Tokens=1* Delims==" %A In ('Set id') Do @Echo Variable %A is %B

Double the % in a batch file.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks it works, but can you explain what's happening in the code above? – Spaniard89 Jun 06 '17 at 10:56
  • `Set id` outputs all variables whose name starts `id`. The `For` loop splits the output into two tokens, `1` being `%A` _(everything up to the first delimiter `=`)_ and `*` being `%B` _(containing everything else after that first delimiter)_. Take a look at `For /?` and `Set /?` for further information. – Compo Jun 06 '17 at 11:49