使用批处理文件写入本地IP地址的文本文件 [英] Write local ip address to text file using batch file

查看:554
本文介绍了使用批处理文件写入本地IP地址的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个txt文件写入本地IP地址的批处理文件。当我运行这个code一个批处理文件(文件路径是错误的,但是这不是问题):

I want to create a batch file that writes the local IP address on a txt file. When i run a batch file with this code (file path is wrong but that's not the problem):

ipconfig | find "IP Address" > \File.txt

我得到了整个窗户IP配置(我改变了一些数字和东西用xxx不显示所有信息)

I get the whole windows IP configuration (I changed some numbers and stuff with xxx to not show all info)

    IP Address. . . . . . . . . . . . : 100.100.100.223

    IP Address. . . . . . . . . . . . : 192.xxx.xxx.xxx

我想写一个文件只 100.100.100.223

我是新来的批处理文件,我真的搜查了很多,我真的很困惑。 (也许是我错了与搜索关键字)。

I'm new to batch files and i really searched a lot and I'm really confused. (maybe i'm searching with wrong keywords).

忘了告诉我使用Windows XP(因为一些问题的答案不会在XP工作)。

Forgot to tell I'm using Windows XP (Because some of the answers doesn't work on XP).

推荐答案

使用设置读取刚刚写入文件的第一行到一个变量/ P 命令,然后覆盖使用该行文件 ECHO

Read the just written file's first line into a variable using the SET /P command, then overwrite the file with that line using ECHO:

ipconfig | find "IP Address" > \File.txt
< \File.txt SET /P first_ip=
ECHO %first_ip%> \File.txt


UPDATE(在回答评论)


UPDATE (in answer to comments):

要只提取IP地址,你可以使用 FOR / F 循环是这样的:

To extract just the IP address, you could use a FOR /F loop like this:

ipconfig | find "IP Address" > \File.txt
< \File.txt SET /P first_ip=
FOR /F "delims=: tokens=2" %%I IN ("%first_ip%") DO (
  ECHO %%I> \File.txt
)

A FOR / F 循环可以让你处理文本(文件或字符串)一行行,分割每行到字段(或标记的)的基础上指定的分隔符(多个)。你可以阅读更多有关在内置帮助这个命令( FOR /?)。我只补充一点,在这种情况下,加工品是一个字符串文字,分隔符设置为 first_ip 的值):和循环变量( %%我)接收2 第二标记,这是一个IP地址。

A FOR /F loop allows you to process text (a file or a string literal) line by line, splitting each line into fields (or tokens) based on the specified delimiter(s). You can read more about this command in the built-in help (FOR /?). I'll only add that in this case the processed piece is a string literal (the value of first_ip), the delimiter is set to :, and the loop variable (%%I) receives the 2nd token, which is an IP address.

注意之间的空间:和IP是preserved,也就是说,它变成了令牌(&NBSP的一部分; 100.100.100.223 ),因此,被写入文件了。如果你不是很高兴的,你可以使用一个子程序摆脱空间的,像这样的:

Note that the space between : and the IP is preserved, i.e. it becomes part of the token ( 100.100.100.223) and, therefore, gets written to the file too. If you aren't very happy about it, you could use a subroutine to get rid of the space, like this:

ipconfig | find "IP Address" > \File.txt
< \File.txt SET /P first_ip=
FOR /F "delims=: tokens=2" %%I IN ("%first_ip%") DO (
  CALL :MyEcho %%I
)
:: the following command is there merely to
:: avoid running (into) the subroutine again
GOTO :EOF

:MyEcho
ECHO %1> \File.txt

在子程序调用( CALL:MyEcho %%我),参数有前导空格一起传递。但处理子程序的命令行的时候,该空间不再被视为价值的一部分,因为该参数没有报价,所以解析器看到仅仅从名字上有两个空格分隔的参数。因此,%1 在子程序的计算结果为地址的没有的前导空格。

When the subroutine is called (CALL :MyEcho %%I), the argument is passed along with the leading space. But when processing the subroutine's command line, that space is no longer seen as part of the value, because the argument is not quoted and so the parser sees merely an argument separated from the name with two spaces. Thus the %1 in the subroutine evaluates to the address without the leading space.

这篇关于使用批处理文件写入本地IP地址的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆