Batch file FOR command skipping lines

8,504
  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.

Share:
8,504

Related videos on Youtube

Nikpie7
Author by

Nikpie7

Updated on September 18, 2022

Comments

  • Nikpie7
    Nikpie7 over 1 year

    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.

    • armani
      armani about 9 years
      Are you running your script in the same directory as Test.txt?
    • BillDOe
      BillDOe about 9 years
      Maybe if you added a full pathname to Test.txt: "C:\Users\Username\Documents\Test.txt" for example.
    • Werner Henze
      Werner Henze about 9 years
      You should write a cd (without params) in the first line to check the current directory. This will differ depending on how and where you start your batch file.