Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Read text file and save to a variable  (Read 11417 times)

0 Members and 1 Guest are viewing this topic.

thomasiancs

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Windows 10
    Read text file and save to a variable
    « on: March 17, 2021, 01:52:40 AM »
    Hi All,

    I'm new to batch and I want to automate my NUnit Script to be dynamic. I'll explain this in a basic question.

    My text file contains

         test1
         test2
         test3

    I want to read those multiple lines and save it into a variable. Here's my current script

    for /f "delims=" %%x in (test.txt) do set params=%%x

    Though I'm only getting the last line which is test3. I want this output:

    test1 test2 test3



    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Read text file and save to a variable
    « Reply #1 on: March 26, 2021, 12:26:21 PM »
    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    set "params="
    For /f "delims=" %%x in (test.txt) do set "params=!params!%%x "

    patio

    • Moderator


    • Genius
    • Maud' Dib
    • Thanked: 1769
      • Yes
    • Experience: Beginner
    • OS: Windows 7
    Re: Read text file and save to a variable
    « Reply #2 on: March 27, 2021, 05:45:28 AM »
    Squashman wow you woke up hungry.... :P

    How ya been ?
    " Anyone who goes to a psychiatrist should have his head examined. "

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Read text file and save to a variable
    « Reply #3 on: March 29, 2021, 07:11:54 AM »
    Squashman wow you woke up hungry.... :P

    How ya been ?
    Pretty Good.  Staying Safe. I doubt any of the original users will come back to all the questions I answered but at least they have an answer for posterity's sake.

    patio

    • Moderator


    • Genius
    • Maud' Dib
    • Thanked: 1769
      • Yes
    • Experience: Beginner
    • OS: Windows 7
    Re: Read text file and save to a variable
    « Reply #4 on: March 29, 2021, 08:31:58 AM »
     ;D    ;)
    " Anyone who goes to a psychiatrist should have his head examined. "