所以我试图使用批处理创建一个数组,我知道,原始的,但是我从头开始学习代码,所以批量是我在跳到之前非常喜欢学习的东西别的什么,反正我知道我可以使用这种语法创建一个数组:
@echo off
set a[0] = 1
set a[1] = 2
set a[2] = 3
set a[3] = my input variable here
我的问题是,如何使用用户输入向阵列添加新行或修改现有行?
我的意思是我想将用户输入变量用作新行或修改数组上的现有行!
答案 0 :(得分:1)
这个例子向您展示如何使用set /p
命令创建和填充数组,以及如何将元素的值修改为数组!
@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
2016年8月1日编辑@ 14:25 :
如何从文本文件中逐行读取数据并将其填充到数组中?
@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
答案 1 :(得分:0)
set /p A[0]=Enter line to replace A[0]
是怎么回事。
这是一个链接,显示如何构建VB.NET程序,而无需使用Windows附带的其他工具。
How to find the window Title of Active(foreground) window using Window Script Host
答案 2 :(得分:0)
所以我在DOS论坛上找到了我需要的东西,这很简单:
< myfile.txt (
set /p line1=
set /p line2=
set /p line3=
set /p line4=
set /p line5=
)
set line
我仍然想修改一些内容,比如添加自动行数增量,所以每次将新字符串添加到文本文件时,它都会自动创建一个新的(set / p行“n”=),所以现在它适用于我想要的东西,它读取指定文本文件中的每一行,并将其放在列表中的变量上,一旦存在,我可以将变量用于我需要的任何内容,不完全是数组,但足够接近,唯一的事情,如果你没有实现“自动行增量”,你必须手动创建空变量,就像我在上面的代码示例中所做的那样,你可以看到我写了“5个变量”,所以如果你输入第6个,那么仍然会将它添加到文本文件中,但是脚本不会列出它,因此需要自动增加空变量。