Windows – Batch file FOR command skipping lines

batchbatch filewindows

I need some help with the FOR command in batch filing. What I want to do is read the file "Test.txt" and read only the third line and turn it into a usable variable within this batch file. Here is what I have attempted but it didn't work.

for /f "skip=2 delims=" %%a in (Test.txt) do (
  set %%a=%LineThree%
)

When I run the program it says "The system cannot find the file Test.txt. All help is much appreciated.

Best Answer

  1. The error indicates that your file Test.txt is not in the same directory you are running your script from.
  2. To better accomplish grabbing just the third line from a text file, try the following, which will run quickly on ANY size file, instead of having to run through an entire file as yours would (and yours would incorrectly... you are actually grabbing every third line).

    (for /l %%a in (1,1,3) do set /p LineThree=) < Test.txt

    echo %LineThree%

As mentioned in another comment, you might want to try the full path to the file instead of just the file name.

Related Question