【问题标题】:Batch file Array creation/modification批处理文件数组创建/修改
【发布时间】:2016-07-30 21:06:16
【问题描述】:

所以我正在尝试使用批处理创建一个数组,我知道,原始的,但我正在从头开始学习代码,所以批处理是我非常喜欢学习的东西,然后再跳到其他东西,无论如何,我知道我可以使用这种语法创建一个数组:

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

我的问题是,如何向数组添加新行或使用用户输入修改现有行?

我的意思是我想使用用户输入变量作为新行或修改数组上的现有行!

【问题讨论】:

  • 不,不,不! “.bat”文件充其量是“必要的邪恶”。从 .bat 文件中学习很容易成为学习“编程”的最糟糕的方式。如果您在 Windows 中需要一些东西……我鼓励使用 C#、VBScript 或 Powershell。一般来说,我鼓励你尝试 C、Java、Swift、C# 或 Python。
  • 传统的“初学者语言”是 Java 和 Python。
  • 您确实有一个 C#、VB.Net 编译器(位于“C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe”)类型 vbc /?。批处理文件旨在启动程序和复制文件。它不是一种语言。我个人不使用它们,我将文本文件中的行剪切并粘贴到 CMD。此外,您还可以下载 MS 编程产品的免费快速版本。
  • 去掉=周围的空格。 Batch 对空格非常挑剔——它们分别成为变量名和值的一部分。使用set /p 要求用户输入(有关更多信息,请参阅set /?)。如果你坚持学习batch-欢迎来到痛苦和头痛的世界;)

标签: arrays batch-file


【解决方案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

编辑于 01/08/2016 @ 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

【讨论】:

  • @paulsm4 你这是什么意思?请更明确地解释你为什么不赞成这个答案!
  • 太棒了!我仍在努力了解这一切是如何协同工作的,但我有一个想法,因为很多参考指南不包含某些命令和运算符,例如 %%i,但我会弄清楚的,我会做一件事很想知道,是如何能够保存这些变量,因为当批处理文件关闭时,添加到数组中的所有变量都被“擦除”,我可以将这些变量保存到 .txt 文件中并稍后调用它们从要添加到数组的批次中?
  • @Wolf 你的意思是如何将数据从文本文件加载到数组并调用它们?
  • 是的!我希望能够输入数据,然后将其保存到文本文件中,这没问题,但是,如果我关闭批处理,然后再次打开它,我希望能够找到该文本文件,从中加载数据(每个文本行)并将每行文本用作批处理文件中的变量,在列表中使用它,明白了吗?我只是想做相当于:echo 1 - Main Menu set /p choice="Enter your choice: " if "%choice%"=="1" goto Main Menu
【解决方案2】:
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

【讨论】:

  • 谢谢你们,我只是喜欢批量编写小脚本,因为它在没有 GUI 的情况下提供了某种程度的界面,我将它用于小时间的东西,例如自动备份,打开快捷方式,现在我正在工作密码加密,让它工作,这个数组元素将成为它的一部分,我知道我仍然可以用 C 编写东西并编译为 .exe,但由于我没有编译器,所以我只是一直在写批处理脚本
  • 但是你确实有一个编译器,总共 18 个 - JS、C#、VB.NET,每个编译器有 6 个版本,它内置在 Windows 中。所有编译器都在这里 C:\Windows\Microsoft.NET\Framework\v4.0.30319 CSC 是 C#,VBC 是 VB.net。
【解决方案3】:

所以我在 DOS 论坛上找到了我需要的东西,很简单:

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

我还是想修改一些东西,比如添加自动行数增量,所以每次在文本文件中添加一个新字符串时,它会自动创建一个新的(set /p line"n"=)所以现在它适用于我想要的东西,它读取指定文本文件中的每一行,并将其放在列表中的一个变量上,一旦存在,我可以将变量用于我需要的任何东西,不完全是一个数组,但足够接近,唯一的一点是,如果你不实现“自动行增量”,你必须手动创建空变量,就像我在上面的代码示例中所做的那样,你可以看到我最多写了“5 个变量”,所以如果你输入第 6 个,它仍会将其添加到文本文件中,但脚本不会列出它,因此需要自动递增空变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多