Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6885007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:34:50+00:00 2026-05-27T05:34:50+00:00

How would I set a each line of a text document to separate variables

  • 0

How would I set a each line of a text document to separate variables using Batch? I know how to set a variable to the first line of a text document using:

Set /p Variable=<Test.txt

…but I don’t know how to read other lines of the file. Lets say for example I had a text document with 3 lines, the first line had ‘Apples’ written on it, the second had ‘Bananas’ and the third had ‘Pears’, and lets say the document was called Fruit.txt. How would I set the variable ‘Line_1’ to the first line of the document, ‘Line_2’ to the second line and ‘Line_3’ to the last line?. Just to keep it simple, lets just say the batch file and Fruit.txt are both in the same folder. I don’t want to do this in VBScript, so please only post Batch code. I would have thought that it would be something like:

@Echo off
Set /p Line_1=<Fruit.txt:1
Set /p Line_2=<Fruit.txt:2
Set /p Line_3=<Fruit.txt:3
Echo Fruit 1 is %Line_1%, Fruit 2 is %Line_2% and Fruit 3 is %Line_3%
Pause
Exit

…but quite clearly it isn’t. Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:34:51+00:00Added an answer on May 27, 2026 at 5:34 am

    EDIT: This is for arbitrary-length files, then. jeb has an answer that solves your particular problem for a known number of lines. I will leave this here, though, as I hate deleting posts I put some time into for explanation 🙂


    Well, you obviously need some sort of counter. Let’s start with 1:

    set Counter=1
    

    Then, you need to go line-wise through the file:

    for /f %%x in (somefile) do ...
    

    Then store the line in a numbered variable (that’s what we have the counter for):

    set "Line_!Counter!=%%x"
    

    aaaaand increment the counter:

    set /a Counter+=1
    

    And that’s it. Add a few more necessary things, you know, the boring stuff that’s always needed in such cases (strange statements before and after, block delimiters, etc.), and you’re done:

    @echo off
    setlocal enabledelayedexpansion
    set Counter=1
    for /f %%x in (somefile) do (
      set "Line_!Counter!=%%x"
      set /a Counter+=1
    )
    set /a NumLines=Counter - 1
    Echo Fruit 1 is %Line_1%, Fruit 2 is %Line_2% and Fruit 3 is %Line_3%
    rem or, for arbitrary file lengths:
    for /l %%x in (1,1,%NumLines%) do echo Fruit %%x is !Line_%%x!
    

    Some explanation:

    • set /p Var=<file will set the variable to the first line of a file, as you noted. That works because set /p will prompt for input and < file will redirect the file into standard input of a command. Thus set /p will interpret the file’s contents as the entered input up until the user hits Return (i.e. the file contains a line break). That’s why you get the first line. The system would throw the whole file at set /p but since the command only reads the first line and then is done they just get discarded.

      The syntax you were proposing there is actually for accessing Alternate Data Streams of files on NTFS, which is somethhing totally different.

      <short-detour> However, jeb has a way of reading multiple lines. This works because the block (delimited by parentheses) is a single command (see below) you can redirect a file’s contents into. Except that command is comprised of multiple statements, each of which will read a single line and store it away. </short-detour>

    • Which brings us to for /f which iterates over the contents of a file (or the output of a command) line by line and executes a command or block of commands for each line. We can now read the whole file into as many variables as there are lines. We don’t even need to know how many in advance.

    • You may have noticed the Line_!Counter! in there which uses Counter a little bit differently from how you’re used to use environment variables, I guess. This is called delayed expansion and is necessary in some cases due to how cmd parses and executes batch files. Environment variables in a command are expanded to their values upon parsing that command. In this case the whole for /f including the block containing two statements is a single command for cmd. So if we used %Counter% it would be replaced by the value Counter had before the loop (1) and never change while the loop is running (as it is parsed once and run multiple times. Delayed expansion (signaled by using ! instead of % for variable access changes that and expands environment variables just prior to running a command.

      This is almost always necessary if you change a variable within a loop and use it within the same loop again. Also this makes it necessary to first enable delayed expansion which is done with the setlocal command and an appropriate argument:

      setlocal enabledelayedexpansion
      
    • set /a will perform arithmetic. We use it here to increment Counter by one for each line read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to execute a stored procedure over each row in a set
I would like to set some initial variables (like format compact and the current
I would like to set up some WMV Video Streaming, using Windows 2003's Streaming
I have this code: File.foreach(testfile.txt) { |line| print line } lines = File.readlines(testfile.txt) lines.each
I would like to remove the unwanted text from each string in a file.
I am parsing a relatively simple text, where each line describes a game unit.
I have a BeanDefinitionDecorator that makes modifications to properties that a user would set
How would I set the overwrite as needed setting on Event logs other than
When would you set location to a URL string versus setting location.href ? location
How would i set the itemdata from getting just the ID/key of a row,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.