1

我正在寻找一种更清洁的方法来做到这一点。我很确定这是错误的,即使它有点工作。基本上我从文件(temp.txt)中获取这样的输出:

    Host Name:                 IBM-3J93A46MRS5
    OS Name:                   Microsoft Windows 7 Professional 
    OS Version:                6.1.7631 Service Pack 1 Build 7601
    OS Configuration:          Standalone Workstation
    Registered Owner:          IAMADMIN
    Original Install Date:     2011-12-15, 10:56:07 AM
    System Boot Time:          2013-05-27, 9:55:36 AM
    System Manufacturer:       DELL
    System Model:              4009BF7
    Time Zone:                 (UTC-05:00) Eastern Time (US & Canada)
    Total Physical Memory:     16,316 MB
    Available Physical Memory: 11,356 MB

并试图将第二列数据放入变量中。取决于我要不要。我正在使用 Findstr 来清除。

    @echo off
    setlocal enabledelayedexpansion

    systeminfo > temp.txt
    set "tmp_result_file=temp.txt"

    for /f "tokens=3-7 delims= " %%i in ('FINDSTR /C:"OS Name:" %tmp_result_file%') do set "OSNAME=%%i %%j %%k %%l"
    for /f "tokens=3-10 delims= " %%i in ('FINDSTR /V /C:"BIOS Version:" %tmp_result_file%^|FIND /I "OS Version:"') do set "OSVER=%%i %%j %%k %%l %%m %%n %%o %%p"
    for /f "tokens=4-7 delims= " %%i in ('FINDSTR /C:"Original Install Date:" %tmp_result_file%') do set "INSTDATE=%%i %%j %%k %%l"
    for /f "tokens=3-7 delims= " %%i in ('FINDSTR /C:"System Manufacturer:" %tmp_result_file%') do set "SYSMFG=%%i %%j %%k %%l"
    for /f "tokens=3-7 delims= " %%i in ('FINDSTR /C:"System Model:" %tmp_result_file%') do   set "SYSMDL=%%i %%j %%k %%l"

    echo.
    echo Operating System      : %OSNAME% , %OSVER% 
    echo Original Install Date : %INSTDATE%
    echo Model Information     : %SYSMFG% %SYSMDL% 

我不喜欢如此精确地使用令牌,我只需要搜索值之后的所有内容。

4

3 回答 3

5

不需要延迟扩展、临时文件或 FINDSTR,当然也不需要计算空格分隔的标记。您只需要设置正确的 DELIMS 和 TOKENS 选项。第一个 FOR /F 处理输出systeminfo并将每一行解析为名称和值。第二个 FOR /F 从每个值中去除前导空格。剩下的就是一系列 IF 语句来测试每个名称并根据需要设置所需的变量。

@echo off
setlocal

for /f "delims=: tokens=1*" %%A in ('systeminfo') do (
  for /f "tokens=*" %%S in ("%%B") do (
    if "%%A"=="OS Name" set "OSNAME=%%S"
    if "%%A"=="OS Version" set "OSVER=%%S"
    if "%%A"=="Original Install Date" set "INSTDATE=%%S"
    if "%%A"=="System Manufacturer" set "SYSMFG=%%S"
    if "%%A"=="System Model" set "SYSMDL=%%S"
  )
)

echo(
echo Operating System      : %OSNAME%, %OSVER%
echo Original Install Date : %INSTDATE%
echo Model Information     : %SYSMFG% %SYSMDL%

如果要捕获更多值,可能需要使用字符串指定搜索字符串:变量名称对,并添加一些额外的 FOR 循环来处理每个测试,如下所示:

@echo off
setlocal

for /f "delims=: tokens=1*" %%A in ('systeminfo') do for %%S in (
  "OS Name:OSNAME"
  "OS Version:OSVER"
  "Original Install Date:INSTDATE"
  "System Manufacturer:SYSMFG"
  "System Model:SYSMDL"
) do for /f "delims=: tokens=1*" %%a in ("%%~S") do if %%A==%%a (
  for /f "tokens=*" %%s in ("%%B") do set "%%b=%%s"
)

echo(
echo Operating System      : %OSNAME%, %OSVER%
echo Original Install Date : %INSTDATE%
echo Model Information     : %SYSMFG% %SYSMDL%
于 2013-06-26T19:46:50.003 回答
5

走简单的路 :-)

@echo OFF &setlocal
FOR /f "tokens=1*delims=:" %%i IN (file) DO (
    FOR /f "tokens=*" %%a IN ("%%j") DO SET "$%%i=%%a"
)
SET "$"
echo Windows version: %$OS Name%

..输出是:

$可用物理内存=11,356 MB
$主机名=IBM-3J93A46MRS5
$原始安装日期=2011-12-15, 10:56:07 AM
$OS 配置=独立工作站
$OS 名称=Microsoft Windows 7 专业版
$OS 版本=6.1.7631 Service Pack 1 Build 7601
$注册所有者=IAMADMIN
$系统启动时间=2013-05-27, 9:55:36 AM
$系统制造商=DELL
$系统型号=4009BF7
$Time Zone=(UTC-05:00) 东部时间(美国和加拿大)
$总物理内存=16,316 MB
Windows 版本:Microsoft Windows 7 专业版
于 2013-06-26T18:28:53.947 回答
0

如果您需要在第三个令牌之后获取其他所有内容,只需在令牌值中使用通配符,如下所示:-

for /f "tokens=3* delims= " %%i in ('FINDSTR /C:"OS Name:" %tmp_result_file%') do set "OSNAME=%%i"

应该显示

Microsoft Windows 7 Professional
于 2013-06-26T18:27:19.483 回答