如何设置从文本文件到字符串变量的每一行? [英] How to set each line from text file to a string variable?

查看:74
本文介绍了如何设置从文本文件到字符串变量的每一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文本文件中的文本对齐.

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包含

file.txt contains,

Rabbit
Cat
Dog
Cow
Pig

test.bat包含

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

运行test.bat

Expected output in cmd window when running test.bat

Rabbit
Cat
Dog
Cow
Pig

但是实际输出正在cmd窗口中

Cat

推荐答案

实现一个计数器,并使用它来生成变量名称,例如str[1] ...:

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[

这篇关于如何设置从文本文件到字符串变量的每一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆