Philip Zx
Philip Zx

Reputation: 15

How to set each line from text file to a string variable?

I'm trying to line up the text in text file.

This is to line up and display each line for the text file. I've tried my own batch program. But its failed.

file.txt contains,

Rabbit
Cat
Dog
Cow
Pig

test.bat contains,

@echo off
cls
for /f "skip=1 tokens=*" %%a In (file.txt) do set "str=%%a" & goto next
:next
echo %str%
pause>nul

Expected output in cmd window when running test.bat

Rabbit
Cat
Dog
Cow
Pig

But the actual output am getting in cmd window,

Cat

Upvotes: 0

Views: 1015

Answers (1)

Stephan
Stephan

Reputation: 56164

implement a counter and use it to generate the variable names like str[1]...:

@echo off
setlocal enabledelayedexpansion
set counter=0
for /f "tokens=*" %%a In (file.txt) do (
  set /a counter+=1
  set "str[!counter!]=%%a"
)
set str[

Upvotes: 2

Related Questions