Need help in creating a batch file which reads a variable value from *.txt file.
需要帮助创建一个从* .txt文件中读取变量值的批处理文件。
Eg.,
MyTxtFile.txt
VAR_A=5
VAR_B=10
MyBatFile.bat Needs to read MyTxtFile.txt and echo the value of VAR_A and VAR_B
MyBatFile.bat需要读取MyTxtFile.txt并回显VAR_A和VAR_B的值
2 个解决方案
#1
1
Depends on how these values have been written. If you can add small "tags" before the values then it makes this job easy and done.
取决于如何编写这些值。如果您可以在值之前添加小“标签”,那么它可以轻松完成这项工作。
For example, here is a text file I made:
例如,这是我制作的文本文件:
random random
VAR-A 2853
VAR-B 1039410
our code wont notice this text
And the batch file ran on it and outputted this:
批处理文件在其上运行并输出:
2853
1039410
Which in the code is written as:
代码中的哪一个写成:
echo %varA%
echo %varB%
Full code is here:
完整代码在这里:
@echo off
cls
cd %~dp0
rem Extracting lines with the tag "VAR-A/B"...
findstr "VAR-A" myfile.txt > Vara.txt
findstr "VAR-B" myfile.txt > Varb.txt
rem Setting the lines to variables...
set /P varA=<Vara.txt
set /P varB=<Varb.txt
rem Deleting temp files...
del Vara.txt
del Varb.txt
rem Now the 2 variables have tags before them, time to remove them...
set varA=%varA:~6%
set varB=%varB:~6%
rem Output:
echo %varA%
echo %varB%
pause>nul
#2
0
You could try the following (see the explanatory rem
comments to learn how it works):
您可以尝试以下方法(请参阅解释性rem注释以了解其工作原理):
@echo off
rem // Define constants here:
set "_FILE=MyTxtFile.txt"
rem // Determine number of lines in text file:
for /F %%C in ('^< "%_FILE%" find /C /V ""') do set "COUNT=%%C"
rem // Read from text file:
< "%_FILE%" (
rem // Loop over number of lines:
for /L %%I in (1,1,%COUNT%) do (
rem // Clear line variable:
set "LINE="
rem // Read current line into variable:
set /P LINE=""
rem // Check whether current line is not empty:
if defined LINE (
rem // Apply current line as variable assignment (avoid messages for invalid ones):
> nul 2>&1 call set "%%LINE%%"
)
)
)
#1
1
Depends on how these values have been written. If you can add small "tags" before the values then it makes this job easy and done.
取决于如何编写这些值。如果您可以在值之前添加小“标签”,那么它可以轻松完成这项工作。
For example, here is a text file I made:
例如,这是我制作的文本文件:
random random
VAR-A 2853
VAR-B 1039410
our code wont notice this text
And the batch file ran on it and outputted this:
批处理文件在其上运行并输出:
2853
1039410
Which in the code is written as:
代码中的哪一个写成:
echo %varA%
echo %varB%
Full code is here:
完整代码在这里:
@echo off
cls
cd %~dp0
rem Extracting lines with the tag "VAR-A/B"...
findstr "VAR-A" myfile.txt > Vara.txt
findstr "VAR-B" myfile.txt > Varb.txt
rem Setting the lines to variables...
set /P varA=<Vara.txt
set /P varB=<Varb.txt
rem Deleting temp files...
del Vara.txt
del Varb.txt
rem Now the 2 variables have tags before them, time to remove them...
set varA=%varA:~6%
set varB=%varB:~6%
rem Output:
echo %varA%
echo %varB%
pause>nul
#2
0
You could try the following (see the explanatory rem
comments to learn how it works):
您可以尝试以下方法(请参阅解释性rem注释以了解其工作原理):
@echo off
rem // Define constants here:
set "_FILE=MyTxtFile.txt"
rem // Determine number of lines in text file:
for /F %%C in ('^< "%_FILE%" find /C /V ""') do set "COUNT=%%C"
rem // Read from text file:
< "%_FILE%" (
rem // Loop over number of lines:
for /L %%I in (1,1,%COUNT%) do (
rem // Clear line variable:
set "LINE="
rem // Read current line into variable:
set /P LINE=""
rem // Check whether current line is not empty:
if defined LINE (
rem // Apply current line as variable assignment (avoid messages for invalid ones):
> nul 2>&1 call set "%%LINE%%"
)
)
)