Batch file FOR command skipping lines
- The error indicates that your file
Test.txt
is not in the same directory you are running your script from. 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 videos on Youtube
Nikpie7
Updated on September 18, 2022Comments
-
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 about 9 yearsAre you running your script in the same directory as
Test.txt
? -
BillDOe about 9 yearsMaybe if you added a full pathname to Test.txt: "C:\Users\Username\Documents\Test.txt" for example.
-
Werner Henze about 9 yearsYou 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.
-