批处理文件未将文件内容分配给变量

Batch file not assigning file contents to variable

我正在写这个批处理文件。我需要将临时文件的内容分配给变量。我已经在同一个文件中完成了它并且它正在运行,所以我不确定为什么它不起作用。

@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 .

我认为事情很复杂......你可以像下面的例子一样使用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= 选项不拆分包含在 '' 符号中的命令的输出。