通过批处理文件将一台机器的UUID和IP存储在一个文件中
Store the UUID and IP of a machine in a file through batch file
我需要通过批处理文件生成一个包含机器 UUID 和 IP 数据的文本文件,如下所示:
CAA8A570-86FF-81E4-3398-0071C21A28CE 192.168.0.0
我使用了这些命令,但我不知道如何将信息放入同一个文件中。
wmic csproduct get "UUID" > C:\UUID.txt
ipconfig /all | find /i "phy" > C:\MAC.txt
可以通过批处理文件获取计算机的IP地址和UUID。但是,尚不清楚您想用它们做什么。因此,我将向您展示获取它们的方法,并将它们的值存储在一个变量中。那就随心所欲吧:
@echo off
rem Script to get the UUID and IP address of this computer.
rem Get IP address:
for /F "tokens=2 delims=(:" %%A IN ('ipconfig /all ^| findstr /c:"IPv4"') do (
for /F "tokens=*" %%B IN ("%%A") do (
rem Set the IP address into the IP_address variable:
set "IP_address=%%B"
)
)
rem Get UUID:
for /F %%A IN ('(wmic csproduct get "UUID"^) ^| findstr /c:"-"') do (
rem Set the UUID in the UUID variable:
set "UUID=%%A"
)
rem Echo the results
echo We have found that this computer has a UUID of %UUID%.
echo We have also found the IP address of this computer. It is: %IP_address%
如果您想将它们重定向到您问题中提到的格式的文件,请使用:
(echo %UUID% %IP_address%)>filename.txt
如果你想附加它们,使用:
(echo %UUID% %IP_address%)>>filename.txt
如果我向您展示每个已处理命令的输出,您可能会更好地理解我的代码是如何工作的。
ipconfig /all | findstr /c:"Ipv4"
:
它的输出是(对我来说):
IPv4 Address. . . . . . . . . . . : xxx.xxx.x.x(Preferred)
我不确定你是否看到这些括号。
处理命令的for /F
循环有tokens=2
和delims=(:
选项。
delims=(:
选项表示不将字符 (
和 :
解析为标记。然后,令牌将是(由 |
分隔):IPv4 Address. . . . . . . . . . . | xxx.xxx.x.x|Preferred)
。如您所见,我已经删除了 (:
字符。包含 IP 地址的令牌是第二个,所以我 select 它使用:
tokens=2
选项。
因为它的开头有一个 space,我在其中做了另一个循环,使用 tokens=*
选项删除了开头的 spaces。
终于将IP地址设置到IP_address
变量中,可以使用了!
(wmic csproduct get "UUID") | findstr /c:"-"
:
这个命令的输出只是UUID(这里没有额外的循环):
F5A91381-529E-11CB-A155-BF406BD05412
所以,我只是将它设置为一个变量(UUID
),就可以使用了!
为了了解'redirect'和'append'的含义,我建议阅读以下链接:
此外,我建议打开一个 cmd,输入以下命令并仔细阅读它们的输出:
for /?
wmic /?
findstr /?
rem /?
set /?
echo /?
这一定会帮助您理解我的代码。
我需要通过批处理文件生成一个包含机器 UUID 和 IP 数据的文本文件,如下所示:
CAA8A570-86FF-81E4-3398-0071C21A28CE 192.168.0.0
我使用了这些命令,但我不知道如何将信息放入同一个文件中。
wmic csproduct get "UUID" > C:\UUID.txt
ipconfig /all | find /i "phy" > C:\MAC.txt
可以通过批处理文件获取计算机的IP地址和UUID。但是,尚不清楚您想用它们做什么。因此,我将向您展示获取它们的方法,并将它们的值存储在一个变量中。那就随心所欲吧:
@echo off
rem Script to get the UUID and IP address of this computer.
rem Get IP address:
for /F "tokens=2 delims=(:" %%A IN ('ipconfig /all ^| findstr /c:"IPv4"') do (
for /F "tokens=*" %%B IN ("%%A") do (
rem Set the IP address into the IP_address variable:
set "IP_address=%%B"
)
)
rem Get UUID:
for /F %%A IN ('(wmic csproduct get "UUID"^) ^| findstr /c:"-"') do (
rem Set the UUID in the UUID variable:
set "UUID=%%A"
)
rem Echo the results
echo We have found that this computer has a UUID of %UUID%.
echo We have also found the IP address of this computer. It is: %IP_address%
如果您想将它们重定向到您问题中提到的格式的文件,请使用:
(echo %UUID% %IP_address%)>filename.txt
如果你想附加它们,使用:
(echo %UUID% %IP_address%)>>filename.txt
如果我向您展示每个已处理命令的输出,您可能会更好地理解我的代码是如何工作的。
ipconfig /all | findstr /c:"Ipv4"
:
它的输出是(对我来说):
IPv4 Address. . . . . . . . . . . : xxx.xxx.x.x(Preferred)
我不确定你是否看到这些括号。
处理命令的for /F
循环有tokens=2
和delims=(:
选项。
delims=(:
选项表示不将字符(
和:
解析为标记。然后,令牌将是(由|
分隔):IPv4 Address. . . . . . . . . . . | xxx.xxx.x.x|Preferred)
。如您所见,我已经删除了(:
字符。包含 IP 地址的令牌是第二个,所以我 select 它使用:tokens=2
选项。
因为它的开头有一个 space,我在其中做了另一个循环,使用 tokens=*
选项删除了开头的 spaces。
终于将IP地址设置到IP_address
变量中,可以使用了!
(wmic csproduct get "UUID") | findstr /c:"-"
:
这个命令的输出只是UUID(这里没有额外的循环):
F5A91381-529E-11CB-A155-BF406BD05412
所以,我只是将它设置为一个变量(UUID
),就可以使用了!
为了了解'redirect'和'append'的含义,我建议阅读以下链接:
此外,我建议打开一个 cmd,输入以下命令并仔细阅读它们的输出:
for /?
wmic /?
findstr /?
rem /?
set /?
echo /?
这一定会帮助您理解我的代码。