批处理脚本 - 创建For循环动态的变量名或数组 [英] Batch Script - Create Dynamic Variable Names or Array in For Loop

查看:1456
本文介绍了批处理脚本 - 创建For循环动态的变量名或数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问问你,如果你可以建议。

I would like to ask you if you can advise.

我不知道是否可以创建在间歇或阵列或类似这样的动态变量名。例如集合C = 0之前,循环,然后设置C = C + 1内循环,并使用该号码内循环创建动态的变量名:例如设置条目%C%='somestring

I don't know if it is possible to create dynamic variable names in batch or array or something like this. For example set c=0 before for loop and then set c = c+1 inside for loop and use this number to create dynamic variable name inside for loop e.g.: set entry%c% = 'somestring'

和以后使用这些编号的变量外的打印输出回路例如:
回声%取值范%
回声%ENTRY2%

and afterwards use these numbered variables to print output outside for loop e.g.: echo %entry1% echo %entry2%

例如:

@echo off

set /a c=1

for %%i in (list.txt) do (

set /a c=c+1
set entry%c% = %%i

)

echo %entry1%
echo %entry2%

感谢您的帮助。

推荐答案

我会使用

for /f "tokens=1*delims=[]" %%a in ('find /n /v "" list.txt') do set "entry%%a=%%b"

但警告约

set entry%c% = %%i

批次是在 SET 语句空间敏感。有了这个工作,它会设置一个命名变量取值范<大骨节病>空间为值<大骨节病>空间 thefirstlineofthefile。

Batch is sensitive to spaces in a SET statement. Had this worked, it would set a variable named "entry1Space" to a value of "Spacethefirstlineofthefile".

我用设置VAR =值语法确保了在该批次线上的任何尾随空格不包括在分配给 VAR 。

The set "var=value" syntax I've used ensures that any trailing spaces on the batch line are not included in the value assigned to var.

这篇关于批处理脚本 - 创建For循环动态的变量名或数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆