如何仅在测试文件中的一行中分配 FOR /F 标记?
如何使其仅从 .txt 文件中的第一行获取令牌,而不是循环遍历每一行。我希望将 %%m 分配给第一行的第三个标记,然后停止。
@echo off
FOR %%A IN (.\xdrive\*.txt) DO (
FOR /F "usebackq tokens=3 delims=," %%m IN ("%%A") DO (
IF "%%m" == "F01" (xcopy /Y "%%A" .\Outbound)
pause
)
)
pause
How do I make this grab the token from the first line ONLY in .txt file instead of looping through every line. I want %%m to be assigned to the 3rd token on line one only then stop.
@echo off
FOR %%A IN (.\xdrive\*.txt) DO (
FOR /F "usebackq tokens=3 delims=," %%m IN ("%%A") DO (
IF "%%m" == "F01" (xcopy /Y "%%A" .\Outbound)
pause
)
)
pause
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
set /p
可以用来读取第一行,然后可以使用 FOR /F 循环来获取第三个标记set /p
can be used to read the first line, and then you can use a FOR /F loop to get the third token如果没有看到eg文件并且确切地知道你想要做什么,我无法测试这个,但这里是
firstline.bat
的列表,它应该满足你的要求:)首先我认为这需要比实际情况更复杂......在第一次调用之后,如果简单地使用 goto 退出 for 结构 - 问题解决了吗?Without see the eg files and knowing exactly what you're trying to do I can't test this, but here's the listing of
firstline.bat
which should do what you're asking for :) At first I thought this needed to be more complicated than it is... after your first if simply use a goto to exit the for structure after it's first call - problem solved?请参阅这篇文章,其中展示了如何使用 dos 批处理文件模拟 gnu
head
实用程序:Windows 批处理命令从文本文件读取第一行
See this post, which shows how to mimic the gnu
head
utility using a dos batch file:Windows batch command(s) to read first line from text file
未经测试的
读取第一行标记3
untested
read first line tokens3
Hackish =
所以你所做的就是在第一遍之后逃避循环,而不是继续下一行。
Hackish =
So all you're doing is escaping the loop after the first pass instead of continuing onto the next line.
该代码读取文件“fileread.txt”的第2行(lineforread),
要更改要读取的行,只需更改 lineforread 变量的值即可。
The code read the line 2 (lineforread) of the file "fileread.txt",
To change the line to read just change the value of the lineforread variable.