我只是在学习批处理脚本的过程中尝试简单循环。在批处理文件中的循环行为不当
for /f "tokens=* delims= " %%a in (abc.txt) do echo %%a
我的abc.txt conatins
word1 word2 word3
现在的输出应该是这样的
word1
word2
word3
但我只是得到一个单一的线,而不是三行。为什么?我在这里做错了什么?
我只是在学习批处理脚本的过程中尝试简单循环。在批处理文件中的循环行为不当
for /f "tokens=* delims= " %%a in (abc.txt) do echo %%a
我的abc.txt conatins
word1 word2 word3
现在的输出应该是这样的
word1
word2
word3
但我只是得到一个单一的线,而不是三行。为什么?我在这里做错了什么?
命令for /f
在每次迭代中逐行处理文本文件abc.txt
。由于eol=;
是默认值,所以空行会被忽略,并且以分号开头。
根据tokens=
和delims=
对所有其他行进行处理对于。
在含有只是word1 word2 word3
命令FOR单个线将向上拆分此行成分配给循环变量子(令牌)abc.txt
情况。分割将与delims=
(只是空格字符)来进行,如下所示:
word1
上作为指定FOR命令行被分配给循环变量a
。word2
根据ASCII table分配给下一个循环变量b
,这是为什么环境变量区分大小写而不区分大小写的环境变量。word3
被分配给下一个循环变量c
。使用tokens=
可以将子串更改为循环变量分配顺序。例如对于tokens=2
,忽略第一个子字符串word1
,将第二个word2
分配给循环变量a
,并且第三个word3
再次被忽略。由于没有至少1个子字符串可以分配给指定的(第一个)循环变量,因此也会忽略仅有1个空格分隔字符串的行。该行必须至少有2个用空格分隔的字符串,tokens=2
用于在循环体中运行命令。
tokens=1*
手段分配word1
到a
和一切第一子串到下一个循环变量b
后而不导致分配word2 word3
到循环变量b
进一步分裂。
tokens=*
表示根本不分割线。所以delims=
没有效果。
两个环所必需的任务与具有在一行3个的单词和在循环体中的命令应该在每行的每个字被执行abc.txt
:
@echo off
for /F "delims=" %%I in (abc.txt) do (
echo Line: %%I
for %%J in (%%I) do echo %%J
)
外环读取非空行不是以文本文件abc.txt
中的分号开始,并将整行分配给循环变量I
。
而不选项内环/F
过程使用利用FOR标准定界符此线无/F
其是空格,制表符,逗号,和其他一些人。
这个批处理文件在文件abc.txt
一行的输出为word1 word2 word3
是:
Line: word1 word2 word3
word1
word2
word3
顺便说一句:在命令提示窗口for /?
上几个显示页输出的帮助此命令运行。由于许多变体,for /F
的行为确实是最难理解的。
感谢您的详细解释。这是很多信息。这纠正了我所知道的。 –
有很多的方式来实现这一点:
@Echo off
Echo Variant 1
for /f "tokens=1-3" %%a in (abc.txt) do echo %%a & echo %%b & echo %%c
Echo Variant 2
For /f "tokens=*" %%a in (abc.txt) do for %%B in (%%a) Do Echo %%B
Echo Variant 3
For /f "tokens=*" %%a in ('type abc.txt') do for %%B in (%%a) Do Echo %%B
输出示例:
Variant 1
word1
word2
word3
Variant 2
word1
word2
word3
Variant 3
word1
word2
word3
谢谢。变体2帮助了我。有没有办法在行之间插入新词? –
你可以追加'&Echo newword' – LotPings
尝试这些版本之一:
版本1(把字的文件中,然后在屏幕上显示,然后显示什么回声):
@echo off
title text file reader/writer
echo This version inputs text that is predefined in the source code into a .txt file.
echo oh hey! I am text line 1>testwords.txt
echo oh hey! I am text line 2>>testwords.txt
echo oh hey! I am text line 3>>testwords.txt
echo oh hey! I am text line 4>>testwords.txt
echo Text entered! displaying below!
type testwords.txt
pause
del testwords.txt
exit
版本2:
@echo off
title text file reader/writer
echo This version displays predefined text in the source code as an "echo"
echo oh hey! I am text!
pause
exit
版本3:
@echo off
title text Displayer
echo This version displays User Entered text as an "echo" via the "set" command.
set /p text=Enter text here:
echo You have entered: %text%
pause
exit
版本4:
@echo off
title text saver/displayer
echo This version saves User Entered text as an "echo" via the "set" command, the "echo" command, and the "type" command.
set /p text=Enter text here:
echo You have entered: %text%>enteredtext.yourtextfile
type enteredtext.yourtextfile
pause
del enteredtext.yourtextfile
exit
这4个“版本”中没有一个能够解决问题中提到的问题,即:使用“简单for循环”读取包含一行的文本文件,其中包含三行,如下所示: 'word1 word2 word3'并在单独的行中显示每个单词。 – Aacini
写''键入abc.txt''代替。此外,不应该是'wordi'的输出在同一行。 – dcg
这部分:''tokens = *“'意味着”在'%% a'字母的输入行中分配**所有标记** ...应该是'for/f“标记= 1-3分隔符=“%% a in(abc.txt)do echo %% a&echo %% b&echo %% c' – Aacini