我正在寻找一种更清洁的方法来做到这一点。我很确定它是错的,即使它有点有用。基本上我从文件(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%
我不喜欢我如此准确地使用令牌,我只需要搜索值之后的所有内容。
答案 0 :(得分: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%
答案 1 :(得分:3)
goi'n轻松的方式:-)
@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%
..输出是:
$Available Physical Memory=11,356 MB $Host Name=IBM-3J93A46MRS5 $Original Install Date=2011-12-15, 10:56:07 AM $OS Configuration=Standalone Workstation $OS Name=Microsoft Windows 7 Professional $OS Version=6.1.7631 Service Pack 1 Build 7601 $Registered Owner=IAMADMIN $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 Windows version: Microsoft Windows 7 Professional
答案 2 :(得分:0)
如果您需要在第3个令牌之后获取其他所有内容,只需在令牌值中使用通配符,如下所示: -
for /f "tokens=3* delims= " %%i in ('FINDSTR /C:"OS Name:" %tmp_result_file%') do set "OSNAME=%%i"
应显示
Microsoft Windows 7 Professional