[英]How to set variables in a Windows batch file from a txt document
Contents of Txt file: Txt文件的内容:
OPS OPS lastmth Jun 2015
OPS OPS currmth Jul 2015
I would like have variables set as: 我想将变量设置为:
var1=OPS
var2=OPS
var3=lastmth
var4=Jun 2015
Is there a way in Windows Batch script to set variables like this? Windows Batch脚本中是否可以设置这样的变量?
I. This is to have var1
to var4
available per line (assuming the text file is called Txt_file.txt
): I.这是使每行var1
到var4
可用(假设文本文件称为Txt_file.txt
):
@echo off
set /A line=0
setlocal enabledelayedexpansion
for /F "tokens=1-3*" %%I in (Txt_file.txt) do (
set /A line+=1
set var1=%%I
set var2=%%J
set var3=%%K
set var4=%%L
rem at this point you have 'var1' to 'var4':
echo LINE !line!:
echo var1: !var1!
echo var2: !var2!
echo var3: !var3!
echo var4: !var4!
)
endlocal
At the rem
line the variables are available. 在rem
行,变量可用。 Expand them like !var1!
像!var1!
一样展开它们!var1!
rather than %var1%
due to delayed expansion (see setlocal /?
and endlocal /?
for more information on that), because we are in the body of the for /F
loop, means in between the parenthesis after do ()
. 而不是由于延迟扩展而导致的%var1%
(有关更多信息,请参见setlocal /?
和endlocal /?
),因为我们位于for /F
循环的主体中,意味着在do ()
之后的括号之间。
The line counter line
is just there for illustration purposes. 线路计数器line
仅用于说明目的。
Outside of the for /F
loop, the variables contain the content of the last line of the text file. 在for /F
循环之外,变量包含文本文件最后一行的内容。
Beyond the setlocal
/ endlocal
block, the variables are no longer available. 除了setlocal
/ endlocal
块之外,变量不再可用。
So the output will be: 因此输出将是:
LINE 1:
var1: OPS
var2: OPS
var3: lastmth
var4: Jun 2015
LINE 2:
var1: OPS
var2: OPS
var3: currmth
var4: Jul 2015
II. 二。 This is to have var1
, var2
and so on throughout the entire text file (up to var8
in your example): 这将在整个文本文件中包含var1
, var2
等(在您的示例中最大为var8
):
@echo off
set /A cnt=0
setlocal enabledelayedexpansion
for /F "tokens=1-3*" %%I in (Txt_file.txt) do (
set /A cnt+=1 & set var!cnt!=%%I
set /A cnt+=1 & set var!cnt!=%%J
set /A cnt+=1 & set var!cnt!=%%K
set /A cnt+=1 & set var!cnt!=%%L
)
rem at this point you have !var1!, !var2!, etc.:
echo ALL CONTENT:
for /L %%I in (1,1,%cnt%) do (
echo var%%I: !var%%I!
)
endlocal & set cnt=%cnt%
echo TOTAL AMOUNT: %cnt%
Again at the rem
line the variables are available. 同样在rem
行,变量可用。 This time I expand them by a for /L
loop because the text file length (number of lines) could differ; 这次我通过for /L
循环来扩展它们,因为文本文件的长度(行数)可能会有所不同。 the number of entries and thus the amount variables is stored in %cnt%
(so you can expand the last variable by !var%cnt%!
, means var8
in our situation). 条目的数量,因此数量变量存储在%cnt%
(因此您可以将最后一个变量扩展!var%cnt%!
var8
在我们的情况下表示var8
)。 You can of course expand the variables like !var1!
当然,您可以扩展变量!var1!
, etc. instead of the for /L
loop; 等等,而不是for /L
循环; this time also %var1%
, etc. works because we are not in the body of the for /F
loop. 这次%var1%
等也可以工作,因为我们不在 for /F
循环的主体中。 Delayed expansion is required though in order to build up the variables dynamically. 为了动态建立变量,需要延迟扩展。
Besides %cnt%
, the variables are no longer available beyond the setlocal
/ endlocal
block. 除了%cnt%
,变量在setlocal
/ endlocal
块之外不再可用。
The output will be: 输出将是:
ALL CONTENT:
var1: OPS
var2: OPS
var3: lastmth
var4: Jun 2015
var5: OPS
var6: OPS
var7: currmth
var8: Jul 2015
TOTAL AMOUNT: 8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.