Batch file 对于批处理文件中的循环错误行为
我只是在学习批处理脚本的过程中尝试简单for循环Batch file 对于批处理文件中的循环错误行为,batch-file,for-loop,Batch File,For Loop,我只是在学习批处理脚本的过程中尝试简单for循环 for /f "tokens=* delims= " %%a in (abc.txt) do echo %%a 我的abc.txt网站 word1 word2 word3 现在输出应该是这样的 word1 word2 word3 但我只得到一行而不是三行。为什么?这里我做错了什么?for/f的命令在每次迭代中逐行处理文本文件abc.txt。空行也被跳过,因为eol=是默认值 所有其他行由根据标记=和delims=处理 如果abc.txt只包
for /f "tokens=* delims= " %%a in (abc.txt) do echo %%a
我的abc.txt网站
word1 word2 word3
现在输出应该是这样的
word1
word2
word3
但我只得到一行而不是三行。为什么?这里我做错了什么?for/f的命令
在每次迭代中逐行处理文本文件abc.txt
。空行也被跳过,因为eol=代码>是默认值
所有其他行由根据标记=
和delims=
处理
如果abc.txt
只包含一行word1 word2 word3
,FOR命令会将该行拆分为分配给循环变量的子字符串(标记)。拆分将使用delims=
(仅空格字符)完成,如下所示:
word1
被分配给循环变量a
,如FOR命令行中所指定
word2
被分配给下一个循环变量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=
没有效果
一行有三个字的任务需要两个循环,循环体中的命令应在每行的每个字上执行:
@echo off
for /F "delims=" %%I in (abc.txt) do (
echo Line: %%I
for %%J in (%%I) do echo %%J
)
外部循环从文本文件abc.txt
读取不以分号开头的非空行,并将整行分配给循环变量I
不带选项/F
的内部循环使用不带/F
的上的标准分隔符(空格、制表符、逗号等)处理此行
在文件abc.txt
的一行中,此批处理文件对word1 word2 word3
的输出为:
Line: word1 word2 word3
word1
word2
word3
顺便说一下:在命令提示符窗口中运行/?
在几个显示页面上输出此命令的帮助。由于变量的数量很多,for/F
的行为确实是最难理解的。命令for/F
在每次迭代中逐行处理文本文件abc.txt
。空行也被跳过,因为eol=代码>是默认值
所有其他行由根据标记=
和delims=
处理
如果abc.txt
只包含一行word1 word2 word3
,FOR命令会将该行拆分为分配给循环变量的子字符串(标记)。拆分将使用delims=
(仅空格字符)完成,如下所示:
word1
被分配给循环变量a
,如FOR命令行中所指定
word2
被分配给下一个循环变量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=
没有效果
一行有三个字的任务需要两个循环,循环体中的命令应在每行的每个字上执行:
@echo off
for /F "delims=" %%I in (abc.txt) do (
echo Line: %%I
for %%J in (%%I) do echo %%J
)
外部循环从文本文件abc.txt
读取不以分号开头的非空行,并将整行分配给循环变量I
不带选项/F
的内部循环使用不带/F
的上的标准分隔符(空格、制表符、逗号等)处理此行
在文件abc.txt
的一行中,此批处理文件对word1 word2 word3
的输出为:
Line: word1 word2 word3
word1
word2
word3
顺便说一句:在一个
@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
@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
@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
@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