The option skip=0
is not accepted by the for /F
command, the specified number must be in the range from 1 to 231 − 1. To skip no lines just do not provide the skip
option at all.
You seem to try to assign the text of a certain line to a variable (for instance, the third one):
FOR /F "skip=2 delims=" %%i IN (values.txt) DO SET @valuethreefinal=%%i
Well, this actually assigns the content of the last line to the variable, because the set
command in the body of the loop is executed for all but the skipped lines. More precisely said, the for /F
loop iterates over all non-empty lines which do not begin with ;
which is the default character of the eol
option.
To actually assign the third line to the variable you need to change the code:
rem // Ensure that the variable is initially unset somewhere before:
set "@valuethreefinal="
rem // As soon as the variable is set the `if` condition is no longer going to be fulfilled:
for /F "usebackq skip=2 delims=" %%i in ("values.txt") do if not defined @valuethreefinal set "@valuethreefinal=%%i"
This does not necessarily assign the third line to the variable, it actually assigns the text of the first line after the (two) skipped ones that is not empty and does not begin with ;
(remember the eol
character).
The usebackq
option allows to put quotation marks around the file name. This is not necessary in your situation, but it is when a file name contains SPACEs or other special characters.
I used the undocumented quoted set
syntax here because this is safer than the unquoted one, particularly when it comes to special characters and also to avoid unintended trailing white-spaces.
To disable the eol
character you could use the undocumented unquoted option string syntax:
for /F usebackq^ skip^=2^ delims^=^ eol^= %%i in ("values.txt") do if not defined @valuethreefinal set "@valuethreefinal=%%i"
As you can see the SPACEs and =
-signs are escaped by the caret symbol ^
in order to treat the whole option string as a unit.
This still skips over empty lines though. To prevent this take a loop at this thread: preserve empty lines in a text file while using batch for /f.
Since you want to capture more than a single line you could extend the code to the following:
set "@valueonefinal=" & set "@valuethreefinal=" & set "@valuethreefinal="
for /F usebackq^ delims^=^ eol^= %%i in ("values.txt") do (
if not defined @valueonefinal (
set "@valueonefinal=%%i"
) else (
if not defined @valuetwofinal (
set "@valuetwofinal=%%i"
) else (
if not defined @valuethreefinal (
set "@valuethreefinal=%%i"
)
)
)
)
This can be compressed to:
set "@valueonefinal=" & set "@valuethreefinal=" & set "@valuethreefinal="
for /F usebackq^ delims^=^ eol^= %%i in ("values.txt") do (
if not defined @valueonefinal (
set "@valueonefinal=%%i"
) else if not defined @valuetwofinal (
set "@valuetwofinal=%%i"
) else if not defined @valuethreefinal (
set "@valuethreefinal=%%i"
)
)
A more flexible method is to use pseudo-arrays:
rem // Initialise an index counter:
set /A "INDEX=0"
rem // Assign every line to an element of a pseudo-array:
for /F usebackq^ delims^=^ eol^= %%i in ("values.txt") do (
rem // Increment the index counter:
set /A "INDEX+=1"
rem // Assign the current line to a pseudo-array element:
call set "@valuefinal[%%INDEX%%]=%%i"
)
The (non-empty) lines of the file value.txt
are now assigned to variables called @valuefinal[1]
, @valuefinal[2]
, @valuefinal[3]
, etc. (there is no concept of arrays in batch scripting, the variables are exactly the same as yours, @valueonefinal
, etc., that is why I use the term "pseudo").
The call
command is used here in order to be able to write and read the variable INDEX
within the same block of code; just using set "@valuefinal[%INDEX%]=%%i"
would result in assigning and therefore overwriting the variable @valuefinal[0]
in every loop iteration.