存储文件输出到变量 [英] Store file output into variable

查看:121
本文介绍了存储文件输出到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的文本文件的输出存储到一个变量,这样我就可以通过整个文件作为参数。<​​/ P>

我使用Windows 2003 Server中。

文本文件有多行这样的:

  10.20.210.100鱼
10.20.210.101岩

我用的是:

 设置/ p VAR =&LT; file.txt的

有只读取该文件的第一行。

我用受审循环也不过只分配的最后一行的变量。


解决方案

有没有简单的方法来获取文件的完整内容为一个变量。
一个变量可以是〜8100长度的限制。
而且它是复杂获得CR / LF到一个变量

但随着一个for循环,你可以得到每个一行。

尝试看看<一个href=\"http://stackoverflow.com/questions/4527877/batch-script-read-line-by-line/4531177#4531177\">batch脚本读取一行一行

编辑:
要在单个变量得到的每一行,你可以使用这个

 关闭@echo
SETLOCAL EnableDelayedExpansion
设定N = 0
FOR / Fdelims =%%一个IN(example.txt中)做(
  集/ A N + = 1
  一套行[!N!= %%一个
  回声线[!N!]是'%%一个

集线

或让所有在只有一个变量(不包括CR / LF)

 关闭@echo
SETLOCAL EnableDelayedExpansion
一套行=
FOR / Fdelims =%%一个IN(example.txt中)做(
  一套行=!行!%%一个

回音!行!

I want to store the output of text file into a variable, so I can pass the whole file as parameter.

I am using Windows 2003 Server.

The text files have multiple lines like:

10.20.210.100 fish
10.20.210.101 rock

I was using:

Set /P var=<file.txt

It reads only the first line of the file.

I tried with FOR loop also but it assigned only the last line to the variable.

解决方案

There is no simple way to get the complete content of a file into one variable. A variable has a limit of ~8100 length. And it is complicated to get CR/LF into a variable

But with a FOR-loop you can get each single line.

Try a look at batch script read line by line

EDIT: To get each line in a single variable you can use this

@echo off
SETLOCAL EnableDelayedExpansion
set n=0
for /f "delims=" %%a IN (example.txt) do (
  set /a n+=1
  set "line[!n!]=%%a"
  echo line[!n!] is '%%a'
)
set line

or to get all in only one variable (without CR/LF)

@echo off
SETLOCAL EnableDelayedExpansion
set "line="
for /f "delims=" %%a IN (example.txt) do (
  set "line=!line! %%a"
)
echo !line!

这篇关于存储文件输出到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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