Recall a Batch Array Using a Variable Visual basic v5.0 includes al...
April 30, 2010 at 08:58:05
Specs: Windows XP
I have used the following code to create a Batch Array.

Set /A Count=1
SET Temp[%Count%]=A
SET /A Count+=1


Command Line: ECHO %Temp[1]%
Output = A

Although what I want to do is to use a variable in place of the "1" in "Temp[1]"

That way I can recall the entire array again
similar to how I originally created it using a Loop and Counter.

1 - Set /A Count=1
2 - :LOOP
3 - ECHO %Temp[%Count%]%
4 - IF %Count%==10 (GOTO EOF) ELSE GOTO LOOP


Line 3: Is where I have the problem. I don't know the correct code/syntax to be able to output the data stored in the array.

The code will only output the name of the array. Any Suggestions?

See More: Recall a Batch Array Using a Variable

Report •

April 30, 2010 at 09:41:37
First, should post this in the programming forum.
Second, what is that you are trying to do? Sounds like you need to us VBS instead of Batch Scripting because it was never ment to be a full blown programming language.
Last, instead of an array why not a file like:

@echo off
rem Write to File
echo 1,A > temp.txt
echo 2,B >> temp.txt
echo 3,C >> temp.txt

rem Read File
for /f "tokens=1,2 delims=," %%a in (temp.txt) do (
  if %%a==2 Echo 2 = %%b

Report •

April 30, 2010 at 11:43:52
If you just want to set a few vars, let's say 10:

@echo off & setLocal EnableDELAYedeXpansion

for /L %%a in (1 1 10) do (
set v%%a=!random!

for /L %%i in (1 1 10) do (
echo.v%%i is !v%%i!

Helping others achieve escape felicity


Report •

April 30, 2010 at 21:09:16
In Response to "ace_omega" the code is useful although I was hoping there is a way to call back the array similar to how you would in VB but using batch file language / code.

Without the need to output the data to a file as I have already successfully managed to create and store data in the array. I just want to be able to re-access the data in the array using a counter for the index rather than the exact index number.

Report •

Related Solutions

April 30, 2010 at 21:18:50
In response to: Mechanix2Go

Unfortunately I want to create an array size greater than 26 items (The array I have made should be basically limitless)

If I was to Echo %Temp[1]% it will output the stored data, so it proves it hold the data.

But rather than using the actual "index" value of "1" I want to be able to use another variable in it's place so that I could increment the variable or even have that variable randomly generated as it Loops.

Report •

April 30, 2010 at 22:21:17
I think M2's code will handle anything within "reason".
I believe you might have misunderstood batch "array" arrangement. batch doesn't use () or []. it just uses expansion. So "array" V looks like: V1, V2, V99, V1000
based on expansion of !i! after V.
So there is no restriction on the number of elements.
so, to test, try:
@echo off & setlocal enabledelayedexpansion
set /a i=0
set x=
set /p x=:how long do you want to keep typing stuff in?
echo my patience will outlast your persistance i bet!!
if "!x!" equ "" goto :test
set /a i+=1
set V!i!=!x!
goto :loop
for /L %%j in (1 1 %i%) do echo V%%j
::---------- end

(note: altho i could use a for /L loop for the first loop, with an arbitrary high value, say, 40000, it seems like it always iterates through all the values!! so it sits there a long time doing nothing... i never noticed that behavior before, but it seems to be the case...)

Report •

May 1, 2010 at 01:06:01
In Response to nbrane & Mechanix2Go

The scalability provided by nbrane on the Mechanix2Go code was very helpful for my understanding.

Both results were most useful and outputs the desired results. Thank You!

Report •

Ask Question