如何仅在测试文件中的一行中分配 FOR /F 标记?

发布于 2024-12-01 06:49:15 字数 268 浏览 2 评论 0原文

如何使其仅从 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦幻的心爱 2024-12-08 06:49:15

set /p 可以用来读取第一行,然后可以使用 FOR /F 循环来获取第三个标记

setlocal EnableDelayedExpansion
FOR %%A IN (%1) DO (
    <%%A set /p firstline=
    FOR /F "tokens=3 delims=," %%m IN ("!firstline!") DO (
        echo %%m
    )
)

set /p can be used to read the first line, and then you can use a FOR /F loop to get the third token

setlocal EnableDelayedExpansion
FOR %%A IN (%1) DO (
    <%%A set /p firstline=
    FOR /F "tokens=3 delims=," %%m IN ("!firstline!") DO (
        echo %%m
    )
)
酒几许 2024-12-08 06:49:15

如果没有看到eg文件并且确切地知道你想要做什么,我无法测试这个,但这里是 firstline.bat 的列表,它应该满足你的要求:)首先我认为这需要比实际情况更复杂......在第一次调用之后,如果简单地使用 goto 退出 for 结构 - 问题解决了吗?

@echo off

::: firstline.bat - Retrieve the first line from a series of files
:::  usage: firstline $filespec
:::     filespace - files to process (eg .\xdrive\*.txt)
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF

FOR %%A IN (%1) DO (
  call :testfirst "%%A"
)
goto :eof

:testfirst
FOR /F "usebackq tokens=3 delims=," %%m IN (%1) DO (
  IF "%%m" == "F01" (xcopy /Y %1 .\Outbound)
  goto:eof
)

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?

@echo off

::: firstline.bat - Retrieve the first line from a series of files
:::  usage: firstline $filespec
:::     filespace - files to process (eg .\xdrive\*.txt)
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF

FOR %%A IN (%1) DO (
  call :testfirst "%%A"
)
goto :eof

:testfirst
FOR /F "usebackq tokens=3 delims=," %%m IN (%1) DO (
  IF "%%m" == "F01" (xcopy /Y %1 .\Outbound)
  goto:eof
)
飞烟轻若梦 2024-12-08 06:49:15

请参阅这篇文章,其中展示了如何使用 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

云淡月浅 2024-12-08 06:49:15

未经测试的

读取第一行标记3

for /f "tokens=3 delims=," %%a in ('"findstr /n . %1|findstr /b 1:"') do set fltok3=%%a
echo(%fltok3%

untested

read first line tokens3

for /f "tokens=3 delims=," %%a in ('"findstr /n . %1|findstr /b 1:"') do set fltok3=%%a
echo(%fltok3%
病女 2024-12-08 06:49:15

Hackish =

@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)
    GOTO:EOF
  )
)

所以你所做的就是在第一遍之后逃避循环,而不是继续下一行。

Hackish =

@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)
    GOTO:EOF
  )
)

So all you're doing is escaping the loop after the first pass instead of continuing onto the next line.

眼眸 2024-12-08 06:49:15

该代码读取文件“fileread.txt”的第2行(lineforread),
要更改要读取的行,只需更改 lineforread 变量的值即可。

@set STDERRZ=fileread.txt
@setlocal EnableDelayedExpansion
@set lineforread=2
@echo off
echo Line %lineforread% for %STDERRZ%
@FOR /F "tokens=*" %%F IN ('more +%lineforread% %STDERRZ%') do (
@SET conteudo=%%F 
goto end)
:end
echo %conteudo%
PAUSE

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.

@set STDERRZ=fileread.txt
@setlocal EnableDelayedExpansion
@set lineforread=2
@echo off
echo Line %lineforread% for %STDERRZ%
@FOR /F "tokens=*" %%F IN ('more +%lineforread% %STDERRZ%') do (
@SET conteudo=%%F 
goto end)
:end
echo %conteudo%
PAUSE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文