Batch Read Line From File Into Variable

Posted on by admin
Batch Read Line From File Into Variable

Is it possible to read in specific lines from a batch file? In this example, I am trying to read the fourth line in the text file. I use an IF-ELSE statement with a GOTO statement to create a loop, but this code does not work.

Batch read line from file into variable 1

Batch Read Line From File Into Variable 1

Read

Batch File Read First Line Of Text File Into Variable

Any assistance is greatly appreciated.set /a LineNumber = 4set FileName = 'c:mpsreportstemp.txt'set /a counter = 0:loopecho%LineNumber%echo%counter%pauseif%LineNumber% %counter% (REM - read line for realSET /p MyLine=<%FileName%clsecho%MyLine% myline.txtpausegoto:end)else(REM - read line and disregardSET /p MyLine=<%FileName%set /a counter=%counter%+1pausegoto:loop):end. When coding For loops (the method used to read text files in batch scripting) you can not access an environment variable that is modified inside the same block sequence without enabling the 'Delayed Expansion' by a 'setlocal' statement.Then the variables marked! Symbol are interpreted as 'dynamic' and expanded at run time while the ones marked by conventional% are expanded before the loop starts and never modified.To avoid confusion you may mark all variables as dynamic when the setlocal is coded (e.g.!FileName! That is the most common pitfall in coding batches where there are sequences of statements embraced by parenthesis.To get a short tutorial on the issue at prompt type 'set /?' To know many interesting things about batch commands type the command code followed by the /? Or call /?).I suggest to follow this road even for well known statements as batch scripting under NT kernel systems (Windows 2000/XP/Vista/7) is more powerful than its native ancestor under plain DOS or Windows 9X/ME environments.