Windows批处理系统信息转换为HTML

3

我正在尝试创建一个批处理文件,使用systeminfo命令将操作系统、当前登录的域、制造商、计算机型号等内容放入HTML表格。这是我当前批处理文件的内容:

@echo off
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /C:"OS Manufacturer" /C:"OS Configuration" /C:"OS Build Type" /C:"Original Install Date" /C:"System Boot Time" /C:"System Manufacturer" /C:"System Model" /C:"System Type" /C:"Processor(s)" /C:"BIOS Version" /C:"Windows Directory" /C:"System Directory" /C:"Boot Device" /C:"System Locale" /C:"Input Locale" /C:"Total Physical Memory" /C:"Available Physical Memory" /C:"Virtual Memory: Max Size" /C:"Virtual Memory: Available" /C:"Virtual Memory: In Use" /C:"Domain" /C:"Network Card(s)"
pause

这是当前的输出:

OS Name:                   Microsoft Windows 7 Professional
OS Version:                6.1.7601 Service Pack 1 Build 7601
Original Install Date:     7/26/2011, 1:47:23 AM
System Boot Time:          2/25/2014, 1:39:14 AM
System Manufacturer:       Dell Inc.
System Model:              Inspiron 1501
System Type:               X86-based PC
Processor(s):              1 Processor(s) Installed.
Domain:                    WORKGROUP
Press any key to continue . . .

我该如何将这个放入HTML表格中?非常感谢您的帮助!谢谢。

可能是使用批处理生成带转义引号的HTML的重复问题。 - phuclv
在Windows命令提示符中转义角括号 - phuclv
2个回答

4
这完全取决于你想要多么高级。最简单的方法是:
@echo off
(
echo ^<HTML^> 
echo ^<BODY^> 
echo ^<pre^> 
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /C:"OS Manufacturer" /C:"OS Configuration" /C:"OS Build Type" /C:"Original Install Date" /C:"System Boot Time" /C:"System Manufacturer" /C:"System Model" /C:"System Type" /C:"Processor(s)" /C:"BIOS Version" /C:"Windows Directory" /C:"System Directory" /C:"Boot Device" /C:"System Locale" /C:"Input Locale" /C:"Total Physical Memory" /C:"Available Physical Memory" /C:"Virtual Memory: Max Size" /C:"Virtual Memory: Available" /C:"Virtual Memory: In Use" /C:"Domain" /C:"Network Card(s)"
echo ^</pre^> 
echo ^</BODY^> 
echo ^</HTML^>
)>sysinfo.html

下面是一种使用CSS格式化表格的方法

@echo off
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /C:"OS Manufacturer" /C:"OS Configuration" /C:"OS Build Type" /C:"Original Install Date" /C:"System Boot Time" /C:"System Manufacturer" /C:"System Model" /C:"System Type" /C:"Processor(s)" /C:"BIOS Version" /C:"Windows Directory" /C:"System Directory" /C:"Boot Device" /C:"System Locale" /C:"Input Locale" /C:"Total Physical Memory" /C:"Available Physical Memory" /C:"Virtual Memory: Max Size" /C:"Virtual Memory: Available" /C:"Virtual Memory: In Use" /C:"Domain" /C:"Network Card(s)">temp.txt
if exist systeminfo.html del /f /q systeminfo.html
call :CreateHTMLtable temp.txt systeminfo.html
if exist temp.txt del /f /q temp.txt
exit /b

:CreateHTMLTable <inputfile> <outputfile>
setlocal
>%2 echo ^<!DOCTYPE HTML PUBLIC 
>>%2 echo "-//W3C//DTD HTML 4.01 Transitional//EN"
>>%2 echo  "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"^>
>>%2 echo ^<HTML^>
>>%2 echo ^<HEAD^>
>>%2 echo ^<META HTTP-EQUIV="Content-Type" 
>>%2 echo CONTENT="text/html; charset=utf-8"^>
>>%2 echo ^</HEAD^>
>>%2 echo ^<BODY^>
>>%2 echo ^<style type="text/css"^>
>>%2 echo .tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #bcaf91;border-collapse: collapse;}
>>%2 echo .tftable th {font-size:12px;background-color:#ded0b0;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91;text-align:left;}
>>%2 echo .tftable tr {background-color:#e9dbbb;}
>>%2 echo .tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91;}
>>%2 echo .tftable tr:hover {background-color:#ffffff;}
>>%2 echo ^</style^>
>>%2 echo ^<table class="tftable" border="1"^>
for /f "tokens=1,2 delims=:" %%a in (%1) do (
>>%2 echo ^<tr^>^<td^>%%a^</td^>^<td^>%%b^</td^>^</tr^>
)
>>%2 echo ^</table^>
>>%2 echo ^</BODY^>
>>%2 echo ^</HTML^>

如何将表格居中,并更改左侧列中的名称(操作系统版本等)?谢谢! - drcomputer

3
以下内容应该适合你。很乐意提供任何解释。
@echo off
(
echo ^<table^>
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /C:"OS Manufacturer" /C:"OS Configuration" /C:"OS Build Type" /C:"Original Install Date" /C:"System Boot Time" /C:"System Manufacturer" /C:"System Model" /C:"System Type" /C:"Processor(s)" /C:"BIOS Version" /C:"Windows Directory" /C:"System Directory" /C:"Boot Device" /C:"System Locale" /C:"Input Locale" /C:"Total Physical Memory" /C:"Available Physical Memory" /C:"Virtual Memory: Max Size" /C:"Virtual Memory: Available" /C:"Virtual Memory: In Use" /C:"Domain" /C:"Network Card(s)">f.txt
for /f "tokens=1* delims=:" %%a in (f.txt) do echo ^<tr^>^<td^>%%a^</td^> ^<td^>%%b^</td^>^</tr^>
echo ^</table^>
) >test.html

显然,将 >>test.html 更改为您要输出文件的名称,我建议使用变量可能是最好的。您还可以通过向 (我使用的) test.html 输出任何 CSS 来为其添加样式。

请注意,在脚本末尾删除 f.txt


+1:但您不需要多次重定向,将所有内容括在括号中,仅重定向一次即可: (echo ^<table... for /f ... echo ^</table ) > test.htm - jeb
@jeb 哈哈,谢谢,那会更方便。我会更新我的答案。 - unclemeat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接