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-22 08:36
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to read text line by line in batch processing without setting it as a variable? View 3,311 Replies 23
Original Poster Posted 2006-04-05 05:19 ·  中国 广东 广州 白云区 电信
初级用户
Credits 100
Posts 34
Joined 2006-04-05 05:13
20-year member
UID 53340
Gender Male
Status Offline
Dear brothers, I'm a novice and want to ask how to read the information in a text file line by line in batch processing and treat it as a variable? For example, the content of file a.txt is three lines, namely "123", "456", "789". How to read them one by one and assign them to three variables A, B, C? That is, A = "123", B = "456", C = "789"?
Floor 2 Posted 2006-04-05 09:13 ·  中国 北京 顺义区 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
for /f "tokens=*" %%i in (a.txt) do (set /a A=123&set /a B=456&set /a C=789)
Floor 3 Posted 2006-04-05 10:14 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
If you want to fully achieve the effect the owner wants, personally, I think it is best to enable extended environment variables and then set automatic increment in the for to achieve the effect; in addition, you can directly use the if statement in the for to implement, the disadvantage is that the code is relatively redundant and the readability is not very good; in addition, you can use goto loop, each time use set /p var=<a.txt to read, and then filter out the read part in a.txt, the disadvantage is that the execution efficiency is very low and the fault tolerance is very poor.

In this way, it seems that it will take some effort to achieve the owner's purpose, but it is not the case. If you are proficient in various commands, you can also achieve the purpose flexibly by using the characteristics of various commands. For example, the findstr command and the find command have a /n parameter, which can add line numbers in front of each line of the displayed search results. Using this line number, maybe we can achieve our requirements. The sample code is as follows:


for /f "delims=: tokens=1,2" %i in ('"findstr /n . a.txt"') do set %i=%j


Note that when running the above code in a batch file, you need to change %i to %%i and %j to %%j first.

Key points explanation:
1. The line number format displayed by findstr is " line number: content ", and the format of find is " content", so use colon ":" to separate in delims.
2. The English half-width period "." after findstr matches all characters in a.txt.
3. If you need to define variables to a, you can add a sentence set a=%1% later, and so on.
Floor 4 Posted 2006-04-05 11:31 ·  中国 广东 广州 白云区 电信
初级用户
Credits 100
Posts 34
Joined 2006-04-05 05:13
20-year member
UID 53340
Gender Male
Status Offline
Can you annotate it on the 2nd floor? I'm a newbie and don't understand it very well. Thanks in advance.
Floor 5 Posted 2006-04-05 11:32 ·  中国 广东 广州 白云区 电信
初级用户
Credits 100
Posts 34
Joined 2006-04-05 05:13
20-year member
UID 53340
Gender Male
Status Offline
Can the third floor provide the code? I can't understand it.
Floor 6 Posted 2006-04-05 11:51 ·  中国 广东 广州 白云区 电信
初级用户
Credits 100
Posts 34
Joined 2006-04-05 05:13
20-year member
UID 53340
Gender Male
Status Offline
The question is how can I reference the value of ABC? For example, how to add the value of ABC to B.TXT, how to achieve it?
Floor 7 Posted 2006-04-05 12:00 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
echo %1% >>b.txt
echo %2% >>b.txt
echo %3% >>b.txt
Floor 8 Posted 2006-04-05 12:17 ·  中国 广东 广州 白云区 电信
初级用户
Credits 100
Posts 34
Joined 2006-04-05 05:13
20-year member
UID 53340
Gender Male
Status Offline
Floor 7, do you mean adding your code to the code of Floor 2, that is:
for /f "tokens=*" %%i in (a.txt) do (set /a A=123&set /a B=456&set /a C=789)
echo %1% >>b.txt
echo %2% >>b.txt
echo %3% >>b.txt

But the result is that the content in b.txt is:
…… (This part is the original content in b.txt)
ECHO is on.
ECHO is on.
ECHO is on.

But the result I want is:
…… (This part is the original content in b.txt)
123
456
789

How to do it?
Floor 9 Posted 2006-04-05 12:23 ·  中国 广东 广州 白云区 电信
初级用户
Credits 100
Posts 34
Joined 2006-04-05 05:13
20-year member
UID 53340
Gender Male
Status Offline
Floor 7, thank you. I've figured it out. It can be like this:
for /f "tokens=*" %%i in (a.txt) do (echo %%i >> b.txt)

Thank you all very much
Floor 10 Posted 2006-04-05 12:26 ·  中国 广东 广州 白云区 电信
初级用户
Credits 100
Posts 34
Joined 2006-04-05 05:13
20-year member
UID 53340
Gender Male
Status Offline
Although I can use this sentence to achieve the goal, I don't fully understand this command. Can someone annotate it for me? Thanks a lot.
Floor 11 Posted 2006-04-05 12:32 ·  中国 广东 广州 白云区 电信
初级用户
Credits 100
Posts 34
Joined 2006-04-05 05:13
20-year member
UID 53340
Gender Male
Status Offline
I mainly don't understand "/F "taken=*", which big brother is willing to help me
Floor 12 Posted 2006-04-05 12:48 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Originally posted by alfredhou at 2006-4-5 05:19:
Dear friends, I'm a newbie, and I want to ask how to read the information in the text file line by line and treat it as a variable in a batch processing? For example, the content of the file a.txt is three lines, respectively "123",...

Depressed, it turned out that it was just to append, not to assign to a variable.
The landlord tells you a simpler one:
type a.txt >>b.txt
Floor 13 Posted 2006-04-05 14:23 ·  中国 广东 广州 白云区 电信
初级用户
Credits 100
Posts 34
Joined 2006-04-05 05:13
20-year member
UID 53340
Gender Male
Status Offline
It's to assign a value to the variable. My purpose is to reference the variable.
Floor 14 Posted 2006-04-05 14:42 ·  中国 广东 广州 白云区 电信
初级用户
Credits 100
Posts 34
Joined 2006-04-05 05:13
20-year member
UID 53340
Gender Male
Status Offline
The code on floor 3 doesn't seem to run. I tried it on WIN2003.
Floor 15 Posted 2006-04-05 14:48 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
1,How do you reference variables A, B, and C respectively after you executed the line for /f "tokens=*" %%i in (a.txt) do (echo %%i >> b.txt)?
2,My code is clearly written below. When running in a batch processing, you need to make modifications. That's just for running in the command line. After running, you can check whether the variables are correctly referenced by echoing %1% %2% %3%.
3,The code has been tested and is indeed executable.
Forum Jump: