2

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
Rudu
  • 15,682
  • 4
  • 47
  • 63
user910557
  • 21
  • 1
  • 3

6 Answers6

2

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
)
Rudu
  • 15,682
  • 4
  • 47
  • 63
  • The `goto next` breaks both FOR loops not only the inner one, and the `:next` label just before the closing bracket results in a syntax error – jeb Aug 24 '11 at 22:31
  • thanks, this looks great. How does the (%1) know to look to look to my *.txt? Since .\xdrive\*.txt is replaced with %1 how does it know to read the text file? – user910557 Aug 25 '11 at 05:56
  • @jeb - Arg the hazards of not testing! Easily solved with a function call instead ;) {Updated} – Rudu Aug 25 '11 at 13:16
  • @user910557 %1 is a reference to the first parameter you call the batch file with. If you actually copy the above code out, save it into `firstline.bat` and execute it it'll tell you you need to provide one parameter (the filespec), in your case you'd want to call it as `>firstline .\xdrive\*.txt`. – Rudu Aug 25 '11 at 13:19
  • So if I have multiple files that I want to pass to it to have the first line read, would I have to add another For statement to the beginning with a call to the batch passing the parameter? – user910557 Aug 25 '11 at 13:32
  • The `filespec` is just like `dir` in that it can match multiple files. You can either (a) call the batch file multiple times with different specs {eg `firstline .\xdrive\*.txt` `firstline .\ydrive\*.txt`} or (b) modify the batch file with a second four loop for another spec (line 8-10 repeat with %2 instead of %1) %2 would be the second parameter you pass {eg `firstline .\xdrive\*.txt .\ydrive\*.txt`}. NOTE: You should enclose your `filespec` in quotes if there are spaces. – Rudu Aug 25 '11 at 13:59
  • A totally different approach would be to have a config file listing all the different FILESPECS (one per line) so the `firstline` process can reference that... it's a fair bit more complicated though – Rudu Aug 25 '11 at 14:00
2

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
    )
)
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Very nice, I was thinking about `SET /P` too. Thanks for saving me the trouble of figuring out the actual solution with it! :) – Andriy M Aug 24 '11 at 23:57
  • Thank u. Same ? There must be something I do not understand about variable expansion or %1. What I %1 and how does this script know to read the .txt file? I have studied up but all I can tell about %1 %2.... is that they represent the 1rst and second parameters. Not sure if that is a line or variable or how it relates to the source *.txt files. Thx again :) – user910557 Aug 25 '11 at 06:25
1

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

Community
  • 1
  • 1
JJ.
  • 5,425
  • 3
  • 26
  • 31
0

untested

read first line tokens3

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

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.

Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
0

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