【发布时间】:2015-09-22 12:41:52
【问题描述】:
我在获取包含 2 行文本的文本文件并将文件的每一行转换为单独的变量时遇到问题。
我想我可以重复相同的过程来获取一行文本,然后在 delims= 旁边添加 skip=1 并更改它进入的变量的名称,这并没有按照我的计划工作,我可以想不出另一种方法来做到这一点。
这是我的代码:
@echo off
for /f "delims=" %%a in (file.txt) do (
set line1=%%a
)
for /f "skip=1 delims=" %%a in (file.txt) do (
set line2=%%a
)
echo %line1%
echo %line2%
pause
文件file.txt里面的文字是这样的:
This is the Text on line 1
This is the Text on line 2
而不是我认为的输出将是 -
This is the Text on line 1
This is the Text on line 2
Press any key to continue...
原来是这样的:
This is the Text on line 2
This is the Text on line 2
Press any key to continue...
它只是为两个回声打印文本文件的第二行!
请回答这个问题,因为我不知道该怎么做真的很困扰我
谢谢,亚历克斯
【问题讨论】:
-
你需要
enabledelayedexpansion -
for /F命令总是读取文件中的所有行。您需要在set line1=%%a之后插入goto命令以中断循环。 -
您听说过 SO 中的搜索功能吗?已经有很多类似的问题了......
标签: windows batch-file cmd