如何在WMIC中搜索产品,如果存在则创建文件

时间:2017-02-24 16:38:37

标签: windows batch-file cmd wmic

我花了很多时间来寻找答案,但仍然无法解决问题

问题: 我需要运行带有fileName的批处理文件,如果我安装了框架,将在我的电脑上检查是否会生成带有框架版本的fileName_1.txt 如果不是我将有空fileName_0.txt

到目前为止我做了什么:

@echo off
set pcName=%1
for /f "tokens=1 delims=1" %%A in ('wmic product where "Name like '%%Microsoft .Net Framework 4.6%%'" get version') do set FrameworksVer=%%A

IF "%frameworksVer%"=="No Instance(s) Available" (
@echo Framework 4.6 Not exist >> c:\%pcName%_0.txt
) ELSE (
@echo %FrameworksVer% >> c:\%pcName%_1.txt
)

每次循环都为变量

分配

答案始终是fileName_1.txt,其中有一对echo ...

我该如何解决?

TNX。

2 个答案:

答案 0 :(得分:1)

解决:

@echo off
set pcName=%1
for /f "tokens=1 delims=1" %%A in ('wmic product where "Name like '%%Microsoft .Net Framework 4.6%%'" get version') do (
if %%A==4.6.1 goto exist
)

goto notExist

:exist
echo Framework Exist > d:\%pcName%_1.txt
exit

:notExist
echo Framework not exist > d:\%pcName%_0.txt
exit

答案 1 :(得分:0)

试试这个。

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
set "pcName=%~1"
Set "Key=HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"
Set "Version="
For /f "tokens=3" %%A in (
 'reg query "%Key%" /v Version ^|find /i "Version" '
) Do Set "Version=%%A"
If not defined Version (Echo could't evaluate .Net Version&Pause&exit /B 1)
If "%Version:~0,4" neq "4.6." (
  echo Framework 4.6 Not exist >> "c:\%pcName%_0.txt"
) ELSE (
  echo %FrameworksVer% >> "c:\%pcName%_1.txt"
)