需要批处理文件帮助 - 函数,变量和子字符串

时间:2021-07-07 00:15:17

I'm trying to write a batch script for windows XP. It needs to build an IP address from ipconfig by getting the (192.168.XXX.30) XXX part of the address and supplanting it into a template. Then some fun stuff from a network shared folder.

我正在尝试为Windows XP编写批处理脚本。它需要通过获取地址的(192.168.XXX.30)XXX部分并将其替换为模板来从ipconfig构建IP地址。然后来自网络共享文件夹的一些有趣的东西。

SET _var=ipconfig |FIND "192.168"
SET _var=%_var:~25,-4%

net use z: \\192.168.%_var%.30\test_folder

... Do stuff

net use z: \DELETE

ECHO "Tasks completed"
PAUSE

At the moment I can't seem to get the result from ipconfig in to my variable, would be even better if I could get the substring from it in 1 line.

目前我似乎无法将ipconfig的结果输入到我的变量中,如果我能从1行获取子串,那就更好了。

Any ideas?

1 个解决方案

#1


0  

The first task, per your question, is to get the IP address. To get one from www.google.com:

根据您的问题,第一项任务是获取IP地址。要从www.google.com获取一个:

for /f "tokens=2 delims=[]" %f in ('ping -4 -n 1 www.google.com ^|find /i "pinging"') do echo IP=%f

Say the resulting value of IP is now 192.168.10.20. You can get the third octet of %IP% from a second FOR statement:

假设IP的结果值现在是192.168.10.20。您可以从第二个FOR语句中获取%IP%的第三个八位字节:

for /f "tokens=3 delims=." %f in ("%IP%") do echo ip3=%f

The result from the above statement, given the value of the example, would be:

鉴于示例的值,上述语句的结果将是:

ip3=10

#1


0  

The first task, per your question, is to get the IP address. To get one from www.google.com:

根据您的问题,第一项任务是获取IP地址。要从www.google.com获取一个:

for /f "tokens=2 delims=[]" %f in ('ping -4 -n 1 www.google.com ^|find /i "pinging"') do echo IP=%f

Say the resulting value of IP is now 192.168.10.20. You can get the third octet of %IP% from a second FOR statement:

假设IP的结果值现在是192.168.10.20。您可以从第二个FOR语句中获取%IP%的第三个八位字节:

for /f "tokens=3 delims=." %f in ("%IP%") do echo ip3=%f

The result from the above statement, given the value of the example, would be:

鉴于示例的值,上述语句的结果将是:

ip3=10