对 FOR 令牌值应用子字符串操作(批处理文件)
(为了清楚起见,编辑了问题)
对 FOR 中的标记值应用子字符串操作不起作用:
for /F "tokens=1 delims=" %%G in ("tokenvalue ") do (
Echo %%G:~-1
)
子字符串操作不会删除末尾的空格。相反, :~-1
被附加到回显结果中以生成:
代币值:~-1
(Edited question for clarity)
Applying substring operation to a token value in a FOR does not work:
for /F "tokens=1 delims=" %%G in ("tokenvalue ") do (
Echo %%G:~-1
)
The substring operation does not delete the space at the end. Instead, :~-1
gets appended to the echoed result to produce:
tokenvalue :~-1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法在这里重现您的问题。仅当我在输入文件中附加空格时,它也会出现在输出文件中。
如果这样做
,显然会附加一个空格。如果您在此处发布之前简化了代码,则可能会出现这种情况。
如果您确实在输入中以空格开始,请使用以下命令:
您仅将子字符串操作应用于环境变量,如帮助中所述。
无论如何,如果您确定输入文件不包含尾随空格,那么您实际上不需要循环。一个简单的
就足够了。
I cannot reproduce your problem here. Only when I append a space to the input file it also appears in the output file.
If you do
then a space gets appended, obviously. This might be the case if you simplified your code before posting here.
If you indeed start out with a space in the input, then use the following:
You apply substring operations only to environment variables as the help already states.
In any case, if you're sure that the input file does not contain the trailing space, then you don't actually need the loop. A simple
should suffice.