China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-23 05:37
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to read data from a specified line in a config file and assign it to a variable? View 898 Replies 6
Original Poster Posted 2009-01-19 11:48 ·  中国 湖北 武汉 电信
初级用户
Credits 40
Posts 17
Joined 2008-08-27 16:32
17-year member
UID 124308
Gender Male
Status Offline
I wrote a batch file myself, and right now I manually modify the variable parameters.
I want to make a config file and have the batch file assign the parameters to variables. What's the best way to handle it?
It would be best if there were an external tool, because the config file is very long, and using for would be very bloated..........
Thanks in advance
Floor 2 Posted 2009-01-19 11:53 ·  中国 广东 深圳 福田区 电信
初级用户
★★
游手好闲 + 无所事事 ..
Credits 194
Posts 167
Joined 2007-04-30 09:43
19-year member
UID 87022
Gender Male
Status Offline
Using for wouldn't be bloated, would it?

Is every line in the config file a variable that needs to be set?
Floor 3 Posted 2009-01-19 11:58 ·  中国 湖北 武汉 电信
初级用户
Credits 40
Posts 17
Joined 2008-08-27 16:32
17-year member
UID 124308
Gender Male
Status Offline
Not every line is a parameter......
Floor 4 Posted 2009-01-19 12:10 ·  中国 广东 深圳 福田区 电信
初级用户
★★
游手好闲 + 无所事事 ..
Credits 194
Posts 167
Joined 2007-04-30 09:43
19-year member
UID 87022
Gender Male
Status Offline
If the file doesn't involve anything confidential, I suggest posting it here as an attachment and then listing your requirements clearly, otherwise people reading it will be completely confused...
Floor 5 Posted 2009-01-19 12:23 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
call :sub
...
...
goto :eof
:sub
for /f "skip=number of lines" %%a in ('file') do (set var=%%a&goto :eof)
::this can fetch one line


set n=1
for /f "skip=number of lines" %%a in ('file') do (set /a n+=1&set var!n!=%%a&if !n!=4 goto :eof)
::this can fetch 4 consecutive lines after the specified line



Also, if there are multiple lines, such as 1 55 88 1223... that need to be processed,
you can first store the corresponding lines as variables, set /a n+=1&if !n!==55 set "_var!n!=%%a"
then for /f %%a in ('set _var') do (perform operations...)
or !_var55! !_var88! !_var1223! will be the contents of those specified lines

[ Last edited by netbenton on 2009-1-19 at 12:42 ]
精简
=> 个人网志
Floor 6 Posted 2009-01-19 12:23 ·  中国 湖北 武汉 电信
初级用户
Credits 40
Posts 17
Joined 2008-08-27 16:32
17-year member
UID 124308
Gender Male
Status Offline
gawk "$3 == 34" val | gawk "/False/ {print $1,$2}" | gawk "$1 > %GeneralLowerLimit% && $1 < %GeneralUpperLimit%" | gawk -f ArithMetic.txt >> Results.txt

This is a section of the processing program. %GeneralLowerLimit% and %GeneralUpperLimit% are parameters the user needs to configure manually. Right now I have the user modify them in the bat, but I want to make a config text file.

The config file is as follows:
#GeneralLowerLimit
11

#GeneralUpperLimit
698
Floor 7 Posted 2009-01-19 12:46 ·  中国 湖北 武汉 电信
初级用户
Credits 40
Posts 17
Joined 2008-08-27 16:32
17-year member
UID 124308
Gender Male
Status Offline
Thanks to the poster in reply #5, I'll go give it a try
Forum Jump: