4

I would like to ask you if you can advise.

I don't know if it is possible to create dynamic variable names in batch or array or something like this. For example set c=0 before for loop and then set c = c+1 inside for loop and use this number to create dynamic variable name inside for loop e.g.: set entry%c% = 'somestring'

and afterwards use these numbered variables to print output outside for loop e.g.: echo %entry1% echo %entry2%

Example:

@echo off

set /a c=1

for %%i in (list.txt) do (

set /a c=c+1
set entry%c% = %%i

)

echo %entry1%
echo %entry2%

Thanks for help.

3
  • Look into delayed expansion variables. setLocal enableDelayedExpansion. Commented Jun 15, 2014 at 23:44
  • 1
    See: Arrays in cmd.exe (batch) script Commented Jun 16, 2014 at 0:50
  • What's inside list.txt? Commented Nov 19, 2021 at 17:08

3 Answers 3

4

I'd use

for /f "tokens=1*delims=[]" %%a in ('find /n /v "" list.txt') do set "entry%%a=%%b"

but a word of warning about

set entry%c% = %%i

Batch is sensitive to spaces in a SET statement. Had this worked, it would set a variable named "entry1Space" to a value of "Spacethefirstlineofthefile".

The set "var=value" syntax I've used ensures that any trailing spaces on the batch line are not included in the value assigned to var.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 This is quite clever. I like the use of find's /v switch to remove the possibility of empty variables, and of course /n to number the variables. A very good one liner.
Great! Thanks - exactly what I've been looking for. I appreciate your help.
3

You need to use delayed expansion variables, otherwise c will always expand to the same value.

You also need to remove the spaces in your set statement. set entry%c% = %%i will create a variable called (for example) entry1<space>, which you would have to expand like so - %entry1 %. So just remove those spaces around the = operator.

@echo off

setLocal enableDelayedExpansion
set c=0

for %%i in (list.txt) do (
    set /a c+=1
    set entry!c!=%%i
)

echo %entry1%
echo %entry2%

Also if you wanted to loop through all the variables you created, you could do something like the following.

for /L %%i in (!c!, -1, 1) do echo !entry%%i!

or

for /L %%i in (1, 1, !c!) do echo !entry%%i!

Comments

0

Here is a snippet I used to list adb devices connected. It assigns each device to a dynamic variable called "device[n]" where n is the index and 1 is the index of the first device in the list.

setlocal enabledelayedexpansion
set i=0
set sum_of_devices=0

REM get list of connected devices
for /f "eol=L tokens=1" %%a in ('adb devices ^| findstr "device"') do (
    set /a i=i+1

    REM device is assigned to dynamic variable 
    for /f "tokens=1" %%b in ('echo device_!i!') do (
        set %%b=%%a
    )
)
echo !device_1!
echo !device_2!
endlocal

Comments

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.