1

so i am trying to make an array using batch, i know, primitive, but i'm learning code from the ground up, so batch is something i'd love very much to learn before i jump to something else, anyways, i know i can create an array using this syntax:

@echo off set a[0] = 1 set a[1] = 2 set a[2] = 3 set a[3] = my input variable here

My question is, how can i add a new line to the array or modify an existing one using user input ?

I mean that i'd like to use a user input variable as a new line or modify an existing line on the array !

7
  • 2
    No, no, NO! ".bat" files are at best a "necessary evil". Learning from .bat files is easily the WORST way to learn "programming". If you need something in Windows ... I'd encourage C#, VBScript or Powershell. In general, I'd encourage you to try C, Java, Swift, C# or Python. Commented Jul 30, 2016 at 21:07
  • 1
    Traditional "beginner languages" are Java and Python. Commented Jul 30, 2016 at 21:21
  • 1
    You do have a C#, VB.Net compiler (at "C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe") type vbc /?. Batch files are designed to start programs and copy files. It is not a language. I personally do not use them, I cut and paste lines from a text file to CMD. Plus you can download free express versions of MS programming products. Commented Jul 30, 2016 at 21:23
  • 1
    See: stackoverflow.com/questions/10166386/… Commented Jul 30, 2016 at 22:19
  • 2
    get rid of the spaces around the =. Batch is very picky about spaces - they become part of the variable name respectively the value. Use set /p to ask user for input (see set /? for more info). If you insist in learning batch - welcome to the world of pain and headache ;) Commented Jul 31, 2016 at 6:23

3 Answers 3

2

This an example to show you how to create and populate an array using set /p command and how to modify the value of an element into the array !

@echo off
setlocal enabledelayedexpansion
Set "StartIndex=0"
Set "EndIndex=3"
Rem To populate the array
for /L %%i in (%StartIndex%,1,%EndIndex%) do (
    echo Write something 
    set /p "Array[%%i]="
)
echo Enter any key to show the values of the elements in the array
pause>nul
Rem To show the values of the elements in the array
For /L %%i in (%StartIndex%,1,%EndIndex%) do (
    echo Array[%%i] = !Array[%%i]!
)
echo Write anything to add and store it as new element 4 in the array
pause>nul 

Set /P "Array[4]="
echo Array[4] = !Array[4]!
echo Write anything to add and store it as new element 5 in the array 
pause>nul

Set /P "Array[5]="
echo Array[5] = !Array[5]! 
echo Hit any key to show the new array with values added
pause>nul

for /L %%i in (0,1,5) do (
    echo Array[%%i] = !Array[%%i]!
)

Rem Modification of an element
echo Write something here to replace Array[3] = !Array[3]!
pause>nul

set /p "Array[3]="
echo The new element is updated as Array[3] = !Array[3]!

echo Hit any key to show the modification
pause>nul
for /L %%i in (0,1,5) do (
    echo Array[%%i] = !Array[%%i]!
)
pause

Edit on 01/08/2016 @ 14:25 :

How to read data line by line from a text file and populate them into an array ?

@echo off
Set "File=%~n0.txt"
echo write your name :
set /p "A[0]="
echo %A[0]% > %File%
cls
echo write your Birtheday :
set /p "A[1]="
echo %A[1]% >> %File%
cls
echo write your gender :
set /p "A[2]="
echo %A[2]% >> %File%
cls
echo Read data line by line from a text file and populate it to an array
echo(
REM Read data line by line from a text file and populate it to an array
setlocal enabledelayedexpansion
set /a i=-1
Rem populate the data readed from the text file into an array
for /f "delims=" %%f in ('Type "%File%"') do (
 set /a i=!i!+1
 set  "A[!i!]"="%%f"
)
set /a lastindex=!i!
for /L %%f in (0,1,!lastindex!) do ( 
  echo "A[%%f]=!A[%%f]!"
)
pause
cls
echo The content of A[0] is : "!A[0]!"
echo The content of A[1] is : "!A[1]!"
echo The content of A[2] is : "!A[2]!" 
pause
cls
set /a "Beforelastindex=!lastindex! - 1"
echo Before last element is : "!A[%Beforelastindex%]!"
echo The last elemnt value is is "!A[%lastindex%]!"
pause
cls
Rem for example edit and modify your birthday and save it in the file text
echo edit and modify your birthday
set /p "A[1]="
(
    for /L %%i in (0,1,%lastindex%) do (
        echo !A[%%i]! 
    )   
)>"%File%"
Start "" "%File%" & exit
Sign up to request clarification or add additional context in comments.

5 Comments

@paulsm4 what did you mean by this ?? Please be more explicit and explain why you downvote this answer !
awesome! i am still working on understanding how it all works together but i got an idea, as a lot of reference guides don't include certain commands and operators, such as %%i, but i will figure it out, one thing i'd love to know, is how to be able to save these variables, as when the batch file is closed, all the variables added to the array are "erased", any way i can save these variables to a .txt file and recall them later from the batch to be added on the array?
@Wolf Did you mean How to load data from a text file to an array and call them ?
yes! i'd like to be able to enter the data, then save it to a text file, that is no problem, but then, if i close the batch, then open it again, i'd like to be able to find that text file, load the data from it (each text line) and use each line of text as a variable in the batch file, to use it on a list, get it? i am just trying to do the equivalent of: echo 1 - Main Menu set /p choice="Enter your choice: " if "%choice%"=="1" goto Main Menu
I piece milled what I needed from this example and it works as I expected.!!
1

so i found what i needed on a DOS forum, it was pretty simple:

< myfile.txt ( set /p line1= set /p line2= set /p line3= set /p line4= set /p line5= ) set line

i still want to modify some things, like adding automatic line count increment, so every time a new string is added to the text file, it will automatically create a new (set /p line"n"=) so right now it works for what i want, it reads each line on the specified text file, and places it on a variable on the list, once is there, i can use the variable for whatever i need, not exactly an array, but close enough, only thing, if you do not implement "automatic line increment" you have to manually create the empty variable, as i did on the code example above, you can see i wrote up to "5 variables" so if you enter a 6th, it will still add it to the text file, but the script won't list it, therefore, the need to automatically increment the empty variables.

Comments

0
set /p A[0]=Enter line to replace A[0]

Is how.

Here's a link showing how to build VB.NET programs without additional tools than what comes with windows.

How to find the window Title of Active(foreground) window using Window Script Host

2 Comments

thank you guys, i just like to write little scripts on batch as it offers interface to some level without GUI,i use it for small time stuff such as automated backups, open shortcuts and now im working on password encryption, which got it to work, and this array element is going to be part of it, i know i can still write stuff in C and compile as an .exe, but as i didn't have a compiler, i have just been writing batch scripts
But you do have a compiler, 18 in total - JS, C#, VB.NET with 6 versions of each compiler, it's built in to Windows. All compilers are here C:\Windows\Microsoft.NET\Framework\v4.0.30319 CSC is C# and VBC is VB.net.

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.