Board logo

标题: [求助]如何在多个文本中提取字段,并添加到另一文本中? [打印本页]

作者: ITU     时间: 2007-6-4 23:46    标题: [求助]如何在多个文本中提取字段,并添加到另一文本中?

如在多台电脑上用"IPCONFIG /ALL >%COMPUTERNAME%.TXT"命令在不同电脑上收集到多个文本文件,如:

user1.txt 的内容如下:
-------------------------------------------------------------------------
Windows IP Configuration
        Host Name . . . . . . . . . . . . : USER1
        Primary Dns Suffix  . . . . . . . : dime
        Node Type . . . . . . . . . . . . : Hybrid
        IP Routing Enabled. . . . . . . . : No
        WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter 本地连接 2:
        Media State . . . . . . . . . . . : Media disconnected
        Description . . . . . . . . . . . : 3Com EtherLink XL 10/100 PCI TX NIC (3C905B-TX)
        Physical Address. . . . . . . . . : 00-01-02-90-48-60
        Dhcp Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 192.168.0.1
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.0.100
        DNS Servers . . . . . . . . . . . : 202.96.134.188
-------------------------------------------------------------------------
;
USER2.TXT的内容:
-------------------------------------------------------------------------

Windows IP Configuration
        Host Name . . . . . . . . . . . . : USER2
        Primary Dns Suffix  . . . . . . . : dime
        Node Type . . . . . . . . . . . . : Hybrid
        IP Routing Enabled. . . . . . . . : No
        WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter 本地连接:
        Media State . . . . . . . . . . . : Media disconnected
        Description . . . . . . . . . . . : Accton EN1207D-TX PCI Fast Ethernet Adapter
        Physical Address. . . . . . . . . : 00-10-B5-BE-28-8D
        Dhcp Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 192.168.0.2
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.0.100
        DNS Servers . . . . . . . . . . . : 202.96.134.188
-------------------------------------------------------------------------

如何通过批处理,将Host Name 、Primary Dns Suffix  、Physical Address、IP Address等字段后的结果提取出来,并添加到一个文本文件中,如以下内容:

Host Name  ;  Primary Dns Suffix  ;Physical Address     ;   IP Address
USER1      ;         dime         ;00-01-02-90-48-60    ;   192.168.0.1
USER2      ;         dime         ;00-10-B5-BE-28-8D    ;   192.168.0.2


[ Last edited by ITU on 2007-6-4 at 11:48 PM ]
作者: lxmxn     时间: 2007-6-5 01:16

@echo off
cd.>result.txt
for %%a in (user*.txt) do (
        (for /f "delims=: tokens=2" %%b in ('findstr /c:"Host Name" /c:"Primary Dns Suffix" /c:"Physical Address" /c:"IP Address" "%%a"') do (
        set/p=%%b        ;<nul
        )
        echo\)>>result.txt
)
start result.txt

作者: ITU     时间: 2007-6-6 01:58
谢谢 lxmxn版主 , 搞定啦!
我还要继续学习FOR命令才行啊, 没想到FOR命令这么好用, 之前写了好长的批处理都没搞定, lxmxn版主一段程序就解决了我的问题啊!