批处理文件未将文件内容分配给变量
Posted
技术标签:
【中文标题】批处理文件未将文件内容分配给变量【英文标题】:Batch file not assigning file contents to variable 【发布时间】:2019-05-12 09:19:59 【问题描述】:我正在编写这个批处理文件。我需要将临时文件的内容分配给一个变量。我已经在同一个文件中完成了它并且它正在工作,所以我不确定它为什么不工作。
@echo off
rem set Microsoft Windows Version value to variable
ver > myVersion.txt
set /p compver = < myVersion.txt
del myVersion.txt
rem set computer hostname value to variable
hostname > compName.txt
set /p myCompName = < compName.txt
del compName.txt
echo Hello %username%, you are currently logged into %myCompName%.
echo It is %time%, on %date%.
echo You are using a PC that is running %compver%
当我运行这个批处理文件时,主机名会显示,但版本不显示。
结果是:
Hello John, you are currently logged into JohnDoe.
it is 3:06:04:43, on Tue 12/11/2018
you are using a PC that is running .
【问题讨论】:
不要在=
-符号前加空格:set /P compver= < myVersion.txt
【参考方案1】:
我认为事情很复杂......您可以使用for /f
来解析您想要的命令的输出,就像以下示例一样:
@echo off
rem set Microsoft Windows Version value to variable
for /f "delims=" %%A IN ('ver') do set "compver=%%A"
rem set computer hostname value to variable
for /f "delims=" %%A IN ('hostname') do set "myCompName=%%A"
echo Hello %username%, you are currently logged into %myCompName%.
echo It is %time%, on %date%.
echo You are using a PC that is running %compver%
rem pause
使用delims=
选项不拆分包含在''
符号中的命令的输出。
【讨论】:
以上是关于批处理文件未将文件内容分配给变量的主要内容,如果未能解决你的问题,请参考以下文章