繁体   English   中英

具有变量的批处理类型文件

[英]Batch type file with variables

我有一个文本文件file.txt,内容是:

%你好%

如果我已将问候语设置为“ holi”,有什么办法可以键入file.txt并解析变量内容?

也许是可回显的可归档管道,但

键入file.txt | 回声

不适合我。

任何帮助将不胜感激。

谢谢

因此,我认为您希望像变量一样扩展文件的内容。

干得好:

>echo %hello%>file.txt

>type file.txt
%hello%

>set hello=holi

>type file.txt
%hello%

>for /f %f in (file.txt) do @echo %f
%hello%

>for /f %f in (file.txt) do @call echo %f
holi

>

假设没有空白行需要保留

for /f usebackq^ delims^=^ eol^= %%A in ("yourFile.txt") do call echo(%%A

如果有空行,但没有行以:开头,则

for /f "delims=: tokens=1*" %%A in ('findstr /n "^" "yourFile.txt"') do call echo(%%B

如果有空行,并且有些行以:开头,则没有! 文字

setlocal enableDelayedExpansion
for /f "delims=" %%A in ('findstr /n "^" "yourFile.txt"') do (
  set "ln=%%A"
  call echo(!ln:*:=!
)

如果有空行,并且有些行以:开头,那么就有! 文字

for /f "delims=" %%A in ('findstr /n "^" "yourFile.txt"') do (
  setlocal enableDelayedExpansion
  set "ln=%%A"
  call echo(!ln:*:=!
  endlocal
)

上面所有方法都使用CALL,这会大大降低速度。 如果您输入!hello! ,解决方案会更快!hello! 在文件中而不是%hello% ,然后可以依靠延迟扩展来扩展变量。

如果没有空行

setlocal enableDelayedExpansion
for /f usebackq^ delims^=^ eol^= %%A in ("yourFile.txt") do echo(%%A

如果为空行,但无行以开头:

setlocal enableDelayedExpansion
for /f "delims=: tokens=1*" %%A in ('findstr /n "^" "yourFile.txt"') do echo(%%B

如果为空行,并且某些行以:

setlocal enableDelayedExpansion
for /f "delims=" %%A in ('findstr /n "^" "yourFile.txt"') do (
  set "ln=%%A"
  echo(!ln:*:=!
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM