Board logo

标题: [网络连接] 【已解决】批处理或者判断的问题 [打印本页]

作者: wh123wh123    时间: 2021-2-18 15:29     标题: 【已解决】批处理或者判断的问题

本帖最后由 wh123wh123 于 2021-2-25 01:25 编辑
  1. @echo off
  2. for /f "tokens=15" %%i in ('ipconfig /all ^| find /i "DHCP Server"') do set WG=%%i
  3. echo DNS 服务器为: %WG%
  4. pause
复制代码
上面这个代码只能在XP老式电脑上正常获取到(因为 ipconfig /all 返回的是英文内容),但是在新的电脑上却不能正常显示(因为返回的dos窗口内容是中文),如何把这个代码修正为可以通用的那种,也就是  "DHCP Server"or"DNS 服务器"
不知道能不能理解我的意思。
作者: Batcher    时间: 2021-2-18 15:38

回复 1# wh123wh123


第一行下面加一行:
chcp 437
试试可以吗
作者: wh123wh123    时间: 2021-2-18 16:16

回复  wh123wh123


第一行下面加一行:
chcp 437
试试可以吗
Batcher 发表于 2021-2-18 15:38


@echo off
chcp 437
...
变成乱码啦
作者: cutebe    时间: 2021-2-18 16:30

  1. ipconfig /all|find /i "DNS"|findstr /i "Server 服务器"
复制代码
  1. ipconfig /all|findstr /i /c:"DNS Server" /c:"DNS 服务器"
复制代码
  1. @echo off
  2. for /f "tokens=2 delims=:" %%i in ('"ipconfig /all|findstr /i /c:"DNS Server" /c:"DNS 服务器""')do set DNS=%%i
  3. set DNS=%DNS: =%
  4. echo DNS 服务器为:[%DNS%]
复制代码
使用findstr的多个/c参数
作者: wh123wh123    时间: 2021-2-18 16:50

使用findstr的多个/c参数
cutebe 发表于 2021-2-18 16:30


下面代码有效果。
c:"DNS Server" /c:"DNS 服务器"  这2个是或者关系还是啥意思?
set DNS=%DNS: =%  这行必须有吗?去掉行不行?这行是啥含义?
作者: wh123wh123    时间: 2021-2-18 16:53

回复 2# Batcher

@echo off
chcp 437>nul
for /f "tokens=15" %%i in ('ipconfig /all ^| find /i "DHCP Server"') do set gateway=%%i
chcp 936>nul
echo 当前网关:%gateway%
用这样的笨办法也行,但是回显这行  echo 当前网关:%gateway%  前面会有一个黑色遮挡了 不知道是咋回事?
作者: Batcher    时间: 2021-2-18 17:01

回复 5# wh123wh123


    是或者关系
作者: wh123wh123    时间: 2021-2-18 17:13

回复 7# Batcher

感谢版主和上面老师的帮助解答,祝福各位老师新春快乐、万事如意,祝福论坛越办越好!
set DNS=%DNS: =%  这行必须有吗?去掉行不行?这行是啥含义?
作者: cutebe    时间: 2021-2-18 17:19

for里面赋值时:DNS=[这里有个空格]10.10.10.x
  1. set DNS=%DNS: =%
复制代码
所以用以上替换来去掉空格。
作者: wh123wh123    时间: 2021-2-18 17:29

回复 9# cutebe

明白了,感谢老师们的解答。
作者: Batcher    时间: 2021-2-18 22:01

回复 8# wh123wh123


推荐阅读 set 命令教程第9页
http://bbs.bathome.net/thread-31727-1-1.html
作者: wh1234567    时间: 2021-2-21 23:10

发现一个问题:
for /f "tokens=2 delims=:" %%i in ('"ipconfig /all|findstr /i /c:"Lease Obtained" /c:"获得租约的时间""')do set Obtained=

%%i
for /f "tokens=2 delims=:" %%i in ('"ipconfig /all|findstr /i /c:"Lease Expires" /c:"租约过期的时间""')do set Expires=%%i
echo 租约的时间为:%Obtained%
echo 过期的时间为:%Expires%

获得的日期时间不完整,例如:实际是 2021年2月22日 0:31:43   但得到的却是 2021年2月22日 0
咋回事
作者: wh1234567    时间: 2021-2-21 23:11

本帖最后由 wh1234567 于 2021-2-21 23:12 编辑

回复 4# cutebe

发现新问题。能不能把关键词后面的行内容完整截取?. . . . . . . . . :分隔符除外
作者: Batcher    时间: 2021-2-22 09:25

回复 12# wh1234567
  1. for /f "tokens=1* delims=:" %%i in ('"ipconfig /all|findstr /i /c:"Lease Obtained" /c:"获得租约的时间""') do (
  2.     set "Obtained=%%j"
  3. )
  4. for /f "tokens=1* delims=:" %%i in ('"ipconfig /all|findstr /i /c:"Lease Expires" /c:"租约过期的时间""') do (
  5.     set "Expires=%%j"
  6. )
  7. echo 租约的时间为:%Obtained%
  8. echo 过期的时间为:%Expires%
复制代码

作者: cutebe    时间: 2021-2-22 10:16

坛主已经给出了答案,我就来解释下吧^_^

for tokens=1* %%i in () do (%%j)
for的用法:1*中1为分隔的第一段,*为后面剩余部分;
第一段指定用I表示,则剩余用J表示。(同理1->a,*->b)




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2