1

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.

  • Are you running your script in the same directory as Test.txt? – armani Apr 15 '15 at 22:21
  • Maybe if you added a full pathname to Test.txt: "C:\Users\Username\Documents\Test.txt" for example. – BillDOe Apr 15 '15 at 22:26
  • 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. – Werner Henze Apr 16 '15 at 12:17
1
  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.

0

The main problem with your code is the missing quotes from in (...)

It should be:

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

If still not works, try to write the full path for Test.txt (as others suggested)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.