|
alfredhou
初级用户
 
积分 100
发帖 34
注册 2006-4-5
状态 离线
|
『楼 主』:
批处理中如何逐行读取文本并非将之设为变量?
使用 LLM 解释/回答一下
各位大哥,我是一名新手,想请教如何在批处理中逐行读取文本文件中的的信息并将之当成变量?例如,文件文件a.txt的内容为三行,分别是“123”、“456”、“789”,如何将它们逐一读出来,并将它赋值到三个变量A、B、C?即A=“123”,B=“456”,C=“789”?
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"?
|
|
2006-4-5 05:19 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
for /f "tokens=*" %%i in (a.txt) do (set /a A=123&set /a B=456&set /a C=789)
for /f "tokens=*" %%i in (a.txt) do (set /a A=123&set /a B=456&set /a C=789)
|
|
2006-4-5 09:13 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
如果要完全达到楼主想要的效果的话,个人认为最好是启用扩展环境变量,然后在for里面设置自动递增,从而达到效果;另外可以直接在for里面用if语句来实现,缺点是代码比较冗余,可读性不是很好;另外还可以goto循环,每次用set /p var=<a.txt来读取,然后再把a.txt中已经读过的部分过滤掉,缺点是执行效率非常低,而且容错很差。
这样看来,似乎达到楼主的目的要花一番工夫了,其实不然,如果对各个命令都能了如指掌的话,利用各种命令的特性也可以变通地达到目的,比如findstr命令和find命令有个/n参数,可以在显示的查找结果每行前面加上行号,利用这个行号,也许能达到我们的要求。示例代码如下:
for /f "delims=: tokens=1,2" %i in ('"findstr /n . a.txt"') do set %i=%j
注意如果在批处理中运行上述代码时需先把%i更改为%%i,%j更改为%%j。
要点说明:
1,findstr显示的行号格式为 " 行号:内容 ",find的格式为 "内容",所以在delims中用冒号":"来分隔。
2,findstr后面的英文半角下句号“.”通配a.txt中的所有字符。
3,如果需要把变量定义到a中可以在后面加上一句set a=%1%,依次类推。
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.
|
|
2006-4-5 10:14 |
|
|
alfredhou
初级用户
 
积分 100
发帖 34
注册 2006-4-5
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
2楼可以注解一下吗?我是新手,看得不太懂。先谢谢啦
Can you annotate it on the 2nd floor? I'm a newbie and don't understand it very well. Thanks in advance.
|
|
2006-4-5 11:31 |
|
|
alfredhou
初级用户
 
积分 100
发帖 34
注册 2006-4-5
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
3楼能给出代码吗?我看不懂呀
Can the third floor provide the code? I can't understand it.
|
|
2006-4-5 11:32 |
|
|
alfredhou
初级用户
 
积分 100
发帖 34
注册 2006-4-5
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
问题是我如何才可以引用ABC的值?比如要把ABC的值加到B.TXT中去,如何实现?
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?
|
|
2006-4-5 11:51 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
echo %1% >>b.txt
echo %2% >>b.txt
echo %3% >>b.txt
echo %1% >>b.txt
echo %2% >>b.txt
echo %3% >>b.txt
|
|
2006-4-5 12:00 |
|
|
alfredhou
初级用户
 
积分 100
发帖 34
注册 2006-4-5
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
7楼,你的意思是不是2楼的代码再加上你的代码,即:
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
但出来的结果,在了b.txt中的内容是:
……(这部分是b.txt原先的内容)
ECHO 处于打开状态。
ECHO 处于打开状态。
ECHO 处于打开状态。
但我想要的结果是:
……(这部分是b.txt原先的内容)
123
456
789
怎么办?
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?
|
|
2006-4-5 12:17 |
|
|
alfredhou
初级用户
 
积分 100
发帖 34
注册 2006-4-5
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
7楼,谢谢啦。我弄明白啦。可以这样:
for /f "tokens=*" %%i in (a.txt) do (echo %%i >> b.txt)
太感谢大家啦
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
|
|
2006-4-5 12:23 |
|
|
alfredhou
初级用户
 
积分 100
发帖 34
注册 2006-4-5
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
我虽然可以用这句达到目的,但我对这条命令理解不透彻,哪位可以注解一下给我吗?不胜感激
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.
|
|
2006-4-5 12:26 |
|
|
alfredhou
初级用户
 
积分 100
发帖 34
注册 2006-4-5
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
我主要是对/F “taken=*"不是很明白,哪位大哥愿意帮帮我
I mainly don't understand "/F "taken=*", which big brother is willing to help me
|
|
2006-4-5 12:32 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
Originally posted by alfredhou at 2006-4-5 05:19:
各位大哥,我是一名新手,想请教如何在批处理中逐行读取文本文件中的的信息并将之当成变量?例如,文件文件a.txt的内容为三行,分别是“123”、 ...
郁闷得紧,原来只是要追加而已,不是要赋值给变量。。
楼主告诉你一条更简单的:
type a.txt >>b.txt
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
|
|
2006-4-5 12:48 |
|
|
alfredhou
初级用户
 
积分 100
发帖 34
注册 2006-4-5
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
是要赋值给变量啊,我的目的是要引用变量啊
It's to assign a value to the variable. My purpose is to reference the variable.
|
|
2006-4-5 14:23 |
|
|
alfredhou
初级用户
 
积分 100
发帖 34
注册 2006-4-5
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
3楼的代码好象运行不了。我在WIN2003上试
The code on floor 3 doesn't seem to run. I tried it on WIN2003.
|
|
2006-4-5 14:42 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
1,你执行了for /f "tokens=*" %%i in (a.txt) do (echo %%i >> b.txt)这一句后你怎么分别引用A,B,C三个变量?
2,我的代码下面写得很清楚了,在批处理中运行时候要最修改,那只是在命令行中运行的,运行完毕后可以通过echo %1% %2% %3%来查看是否正确引用到了变量。
3,代码经过测试,确实可执行。
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.
|
|
2006-4-5 14:48 |
|