![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-23 22:53 |
47,812 topics / 349,917 posts / today 0 new / 48,274 members |
| DOS批处理 & 脚本技术(批处理室) » How to read text line by line in batch processing without setting it as a variable? |
| Printable Version 3,737 / 23 |
| Floor1 alfredhou | Posted 2006-04-05 05:19 |
| 初级用户 Posts 34 Credits 100 | |
|
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"?
|
|
| Floor2 bagpipe | Posted 2006-04-05 09:13 |
| 银牌会员 Posts 425 Credits 1,144 From 北京 | |
|
for /f "tokens=*" %%i in (a.txt) do (set /a A=123&set /a B=456&set /a C=789)
|
|
| Floor3 3742668 | Posted 2006-04-05 10:14 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
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: 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. |
|
| Floor4 alfredhou | Posted 2006-04-05 11:31 |
| 初级用户 Posts 34 Credits 100 | |
|
Can you annotate it on the 2nd floor? I'm a newbie and don't understand it very well. Thanks in advance.
|
|
| Floor5 alfredhou | Posted 2006-04-05 11:32 |
| 初级用户 Posts 34 Credits 100 | |
|
Can the third floor provide the code? I can't understand it.
|
|
| Floor6 alfredhou | Posted 2006-04-05 11:51 |
| 初级用户 Posts 34 Credits 100 | |
|
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?
|
|
| Floor7 3742668 | Posted 2006-04-05 12:00 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
echo %1% >>b.txt
echo %2% >>b.txt echo %3% >>b.txt |
|
| Floor8 alfredhou | Posted 2006-04-05 12:17 |
| 初级用户 Posts 34 Credits 100 | |
|
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? |
|
| Floor9 alfredhou | Posted 2006-04-05 12:23 |
| 初级用户 Posts 34 Credits 100 | |
|
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 |
|
| Floor10 alfredhou | Posted 2006-04-05 12:26 |
| 初级用户 Posts 34 Credits 100 | |
|
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.
|
|
| Floor11 alfredhou | Posted 2006-04-05 12:32 |
| 初级用户 Posts 34 Credits 100 | |
|
I mainly don't understand "/F "taken=*", which big brother is willing to help me
|
|
| Floor12 3742668 | Posted 2006-04-05 12:48 |
| 荣誉版主 Posts 718 Credits 2,013 | |
Originally posted by alfredhou at 2006-4-5 05:19: 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 |
|
| Floor13 alfredhou | Posted 2006-04-05 14:23 |
| 初级用户 Posts 34 Credits 100 | |
|
It's to assign a value to the variable. My purpose is to reference the variable.
|
|
| Floor14 alfredhou | Posted 2006-04-05 14:42 |
| 初级用户 Posts 34 Credits 100 | |
|
The code on floor 3 doesn't seem to run. I tried it on WIN2003.
|
|
| Floor15 3742668 | Posted 2006-04-05 14:48 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
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. |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |