0

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
Philip Zx
  • 15
  • 7
  • The command you have written should output the information you have shown us. Open a Command Prompt window and enter `for /?` to read the usage information for the command you are having an issue with. – Compo Jun 26 '19 at 13:29
  • 1
    That's because you break the loop after the first line with `goto`. Try `for /f "skip=1 tokens=*" %%a In (file.txt) do echo %%a` – Stephan Jun 26 '19 at 13:29
  • 1
    If you were wanting the first line, _(not beginning with a semicolon)_, there's also no need to use `Skip`. – Compo Jun 26 '19 at 13:32
  • That works and its displaying the output. But, Actually i want to display like, `echo 1 - %var1% echo 2 - %var2% echo 3 - %var3% echo 4 - %var4% echo 5 - %var5%` each line in different varible. Kindly help me to set this up. – Philip Zx Jun 26 '19 at 13:46
  • So you basically want to have array-like variables, hence you should take a look at [this great post](https://stackoverflow.com/a/10167990)... – aschipfl Jun 26 '19 at 14:34

1 Answers1

2

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[
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Stephan
  • 53,940
  • 10
  • 58
  • 91