简体   繁体   English

如何使用批处理逐行读取文件

[英]How to read file line by line using batch

Source file is present in below manner:- 源文件以以下方式存在:-

abc ABC

dfc DFC

adbc 中国农业发展银行

I am using below code to print the each line in the file. 我正在使用下面的代码来打印文件中的每一行。

for /f "tokens=* delims=" %%a in ('type input.txt') do (

set line=%%a
echo %line%
)

but the output is 但输出是

adbc 中国农业发展银行

adbc 中国农业发展银行

adbc 中国农业发展银行

What to do? 该怎么办? Required output is: 所需的输出是:

abc ABC

dfc DFC

adbc 中国农业发展银行

除非您特别需要操纵该行或将最后一行的内容保存在变量中,否则绝对不需要使用For循环:

Type input.txt

@Squashman provided the answer really, but here it is written out: @Squashman确实提供了答案,但是在这里写出来:

setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in ('type input.txt') do (
set line=%%a

echo !line!
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何逐行读取批处理文件中多余字符的文本文件? 允许限制行长。(Windows,批处理脚本) - How to read text file line by line which is excessing characters in batch file? Limiting the line length is allowed.(Windows, batch script) 使用IF-批处理文件将一行替换为另一行 - Replace a line with another line using IF - Batch file 如何从第x行到第y行读取文件(dos / win批处理文件) - how to read a file from line x to line y (dos/win batch file) 读取一行文本并将其设置为批处理文件的变量 - Read a line of text and set it as a variable for a batch file 如何使用行号读取文件行内容 - How can I read a file line content using line number 从同一个批处理文件中读取批处理文件的第一行? - Read the first line of batch file from the same batch file? 批处理文件-读取特定行,并将该行中的特定字符串另存为变量 - Batch File - Read specific line, and save a specific string in that line as a variable 如何使用批处理文件命令打印文本文件中的部分行 - How to print part of the line in a text file using batch file commands 如何在WindowsXP上使用批处理文件向文本文件添加行号 - How to add line numbers to a text file using batch file on WindowsXP 如何使用批处理从文件的第一行提取字符串? - How to extract a string from the first line of a file using batch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM