【问题标题】:Batch - Echoing file without expanding the variables inside the file批处理 - 回显文件而不扩展文件内的变量
【发布时间】:2017-10-23 05:41:32
【问题描述】:

这可能与this question 完全相反。

无论如何,我有一个包含此类内容的批处理文件:

a.bat

@echo %random%

我尝试通过另一个批处理文件读取 a.bat

for /f "delims=" %%p in ('dir *.bat') do (
    rem loop through files
    for /f "delims=" %%q in ('type %%~p') do echo %%q 
) 2>nul

它输出:

@echo (random number)

我怎样才能让它输出@echo %random%?任何帮助将不胜感激。

【问题讨论】:

  • 我不确定你在测试什么,但你描述的结果不能是你代码的输出
  • 很奇怪。在我的 Win7 上,它输出 @echo (the random number)
  • 总是同一个数字吗? x.bat 的真正内容是什么?
  • 数字并不总是与%random% 相同。真正的内容太长了,不适合这里,但无论如何我使用了另一种解决方法并删除了旧脚本:(

标签: windows file batch-file variables for-loop


【解决方案1】:

您的代码在这里工作(win 8)。只添加了/B 切换到 dir 命令。

for /f "delims=" %%p in ('dir /B a.bat') do (
    rem loop through files
    for /f "delims=" %%q in ('type %%~p') do echo %%q 
) 2>nul

不过你也可以试试

SetLocal DisableDelayedExpansion
for /f "delims=" %%p in ('dir /B a.bat') do (
  for /F "tokens=1,* delims=[]" %%1 in ('"type "%%~p"|find /N /V """') do echo/%%2
)
EndLocal

在我的电脑里都给@echo %ramdon%

【讨论】:

  • 最后一句中的错字......顺便说一句,第二个解决方案就像一个魅力:)
  • 当一行以[]? 开头时,它会失败。另见SO:How to read a file
猜你喜欢
  • 1970-01-01
  • 2012-07-19
  • 2017-06-11
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
相关资源
最近更新 更多