多行文本文件,如何投入到环境变量 [英] Multiline text file, how to put into an environment variable
问题描述
我有一个名为file.txt的文件,其中包含:
i have a file called file.txt which contains:
this is line one ^
this is line two ^
this is the last line
我怎样才能把那个到的环境变量?
how can i put that into an env var?
我可以从一个批处理文件这样做:
test.bat的
i can do this from a batch file: test.bat
set LF=^
[blank line]
[blank line]
rem two blank lines needed above
set multi=Line 1!LF!Line 2!LF!Last line
echo !multi!
这个输出三行:
Line 1
Line 2
Last line
所以如何我可以将file.txt的一个ENVVAR批处理文件里面呢?
so how can i get file.txt into envvar inside a batch file?
推荐答案
由于dbenham说,可以也做FOR / F,但它更复杂一些。
As dbenham said, it can be done also with for/f but it's a bit more complicated.
简单的80%的溶液
setlocal EnableDelayedExpansion
set "var="
set LF=^
rem *** Two empty lines are required for the linefeed
FOR /F "delims=" %%a in (myFile.txt) do (
set "var=!var!!LF!%%a"
)
echo !var!
但它失败:结果
- 如果行是空将跳过结果
- 如果符合开始;
的EOL字符结果
- 如果一行包含
(插入记号和)
But it fails with:
- If a line is blank it will be skipped
- If a line begins with ;
the EOL-character
- If a line contains !
(and carets)
不过,你可以用一个稍微复杂的解决方案。
But then you could use a bit more complex solution
@echo off
SETLOCAL DisableDelayedExpansion
set "all="
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ aux1.txt"`) do (
set "line=%%a"
SETLOCAL EnableDelayedExpansion
set "line=!line:#=#S!"
set "line=!line:*:=!"
for /F "delims=" %%p in ("!all!#L!line!") do (
ENDLOCAL
set "all=%%p"
)
)
SETLOCAL EnableDelayedExpansion
if defined all (
set "all=!all:~2!"
set ^"all=!all:#L=^
!"
set "all=!all:#S=#!"
)
echo !all!
在code做些什么?结果
首先, FINDSTR / N ^^
将prePEND的行号和冒号,像
What the code do?
First, the findstr /n ^^
will prepend each line with a line number and a colon, like
1:My first Line
2:; beginning with a semicolon
3:
4:there was an empty line
这解决了空行,也是标准的EOL字符的问题;
可以忽略不计结果。
要得到行的内容,将值设置为一个变量,而延迟扩展被禁用,这解决了问题,!
和 ^
字符。
This solves the problem of empty lines and also the standard EOL-character ;
can be ignored.
To get the content of the line, the value is set to a variable while delayed expansion is disabled, this solves the problem with !
and ^
characters.
要删除的行号和冒号,延迟扩展将被启用(不,一个DELIM :
解决不了)。
To remove the line number and the colon, the delayed expansion will be enabled (no, a delim of :
can't solve it).
然后,所有#
被替换为 #S
,这将是第一次做,因为$ P $之后PFIX删除线可能是空的,替换将失败。结果
但为什么我更换呢?结果
这是因为我不能在这里插入换行,如下面的FOR / F将失败,并嵌入换行,结果
所以我只添加的换行标记的(在这种情况下,我用 #L
),但文件的内容也包含 #L
,而是通过替换所有#
与 #S
所有标记都是独一无二的。
Then all #
are replaced with #S
, this will be done first, as after the prefix removing the line could be empty and the replacement would fail.
But why I replace it?
That's because I can't insert the linefeeds here, as the following FOR/F would fail with embedded linefeeds,
so I only add linefeed marker (in this case I use #L
), but the content of the file could contain also a #L
, but by replacing all #
with #S
all markers are unique.
标记后,是关闭/禁止与 ENDLOCAL
,但preserve延迟扩展修改所有
和行
变量。结果
这与 FOR / F-ENDLOCAL
的把戏,因为 %% P
可输送背后的内容<$完成C $ C> ENDLOCAL 屏障。
After the marker, there is the problem to close/disable the delayed expansion with an endlocal
, but preserve the content of the modified all
and line
variable.
This is done with the FOR/F-endlocal
trick, as the %%p
can transport content behind the endlocal
barrier.
然后读取完整的文件后,我检查所有
的定义,因为这将是空的空文件。结果
然后第一个的换行标记的 #L
将被删除,而所有其他标记与真正的换行符替换。结果
那么犀利安全 #S
将恢复到#
。
Then after reading the complete file, I check if the all
is defined, as it would be empty for an empty file.
Then the first linefeed marker #L
will be removed, and all other markers are replaced with a real linefeed character.
Then the sharp safer #S
will be reverted to #
.
这就是全部,所以即使这个解决方案显然是...
That's all, so even this solution is obviously...
这篇关于多行文本文件,如何投入到环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!