China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-25 05:06
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Problems with delay variables View 1,681 Replies 5
Original Poster Posted 2010-12-06 14:46 ·  中国 浙江 台州 电信
初级用户
Credits 55
Posts 29
Joined 2006-04-11 16:40
20-year member
UID 53669
Gender Male
From 亚洲
Status Offline
@echo off&setlocal enabledelayedexpansion
for /f "usebackq tokens=2 delims=:" %%i in (`ipconfig ^| find "Default Gateway"`) do set gw=%%i
for /f "usebackq tokens=2 delims= " %%i in (`arp -a ^| findstr "\<!gw!\>"`) do echo %%i
pause

Why can getting the MAC address of the gateway work when using `findstr "\<10.0.0.1\>"`
but not when using the delayed variable !gw!?

[ Last edited by jun19841 on 2010-12-6 at 14:58 ]
Floor 2 Posted 2010-12-06 15:12 ·  中国 吉林 延边朝鲜族自治州 敦化市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Look at my place http://www.cn-dos.net/forum/viewthread.php?tid=52933&fpage=2
The explanation on floor 11!~
It's because you have already brought the space in when setting gw=%%i
The result is "space + Default Gateway"
The correct way should be written like this:
@echo off
for /f "tokens=2 delims=:" %%i in (`ipconfig ^| find "Default Gateway"`) do set gw=%%i
for /f "tokens=2 delims= " %%i in (`arp -a ^| findstr "\<%gw:~1%\>"`) do echo %%i
pause

%gw:~1% is used to remove the leading space that was brought in
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
jun19841 +2 2010-12-06 15:20
Floor 3 Posted 2010-12-06 15:20 ·  中国 浙江 台州 电信
初级用户
Credits 55
Posts 29
Joined 2006-04-11 16:40
20-year member
UID 53669
Gender Male
From 亚洲
Status Offline
Thanks to Hanyeguxing for the guidance, I've learned it~
Floor 4 Posted 2010-12-06 15:21 ·  中国 浙江 台州 电信
初级用户
Credits 55
Posts 29
Joined 2006-04-11 16:40
20-year member
UID 53669
Gender Male
From 亚洲
Status Offline
Originally posted by Hanyeguxing at 2010-12-6 15:12:
Check the explanation on floor 11 of http://www.cn-dos.net/forum/viewthread.php?tid=52933&fpage=2!~
You already included the space when you did set gw=%%i
The result is "space + Default Gateway ...



Hmm, inserting call can also be used to call, learned~
Floor 5 Posted 2010-12-06 15:24 ·  中国 浙江 台州 电信
初级用户
Credits 55
Posts 29
Joined 2006-04-11 16:40
20-year member
UID 53669
Gender Male
From 亚洲
Status Offline
for /f "usebackq tokens=2 delims= " %%i in (`arp -a ^| findstr "\<call !gw!\>"`) do echo %%i
for /f "usebackq tokens=2 delims= " %%i in (`arp -a ^| findstr "\<!gw:~1!\>"`) do echo %%i
Floor 6 Posted 2010-12-25 12:31 ·  中国 广东 东莞 中移铁通
初级用户
★★
Credits 115
Posts 92
Joined 2010-12-24 16:57
15-year member
UID 179794
Gender Male
Status Offline
Learning
Forum Jump: