0

A list of variables come through a call to another .Cmd file. The variables follow a pattern Item1, Item2, Item3, ...

I would like to loop through the unknown number of Item* and do some operation but I don't know how to do it. Your help is much appreciated.

Pseudo code

  • Get all Item* variables
  • Perform a foreach with it.
InquisitiveLad
  • 309
  • 3
  • 16
  • 1
    Possible duplicate of [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script) – aschipfl Aug 01 '17 at 15:17

2 Answers2

3

set item lists all your variables (Attention: sorted alphanumeric)

You can use a for loop to do something with them:

for /f "tokens=1* delims==" %%a in ('set item') do echo -- %%b

If you need to keep the order, count them first and use a for /l:

@echo off    
setlocal enabledelayedexpansion
for /f %%a in ('set item^|find /c /v ""') do set count=%%a
for /l %%a in (1,1,%count%) do (
  echo -- item%%a = !item%%a!.
)
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

Since you said that you want to "perform a foreach" on them...

powershell -NoProfile -Command "Get-ChildItem Env:PROC* | ForEach-Object { $_.Name }"
lit
  • 14,456
  • 10
  • 65
  • 119