开发者

How do I only assign FOR /F token from one line only in a test file?

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


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
    )
)


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
)


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


untested

read first line tokens3

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜