Reputation: 125
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 !
Upvotes: 1
Views: 1479
Reputation: 125
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.
Upvotes: 1
Reputation: 18827
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
Upvotes: 2
Reputation:
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
Upvotes: 0