Well,首先,前面两句建议还是合并为一句:
ipconfig /all | find "Ethernet adapter" >connect.txt
当然也可以直接在for /f里面使用而不产生临时文件。
其次,如果你想得到 (本地连接 2),那么,你可以在启用延迟环境变量(setlocal ENABLEDELAYEDEXPANSION)后,在for的do语句里面把"本地连接 2:"赋值到变量connect,然后通过输出!connect:~0,-1!的方法来去掉最后的那个冒号。
当然,这个问题并非一定要用for才能解决,很多时候,我们应该放开我们的思路:
@echo off
set "connect=本地连接"
ipconfig /all | find "Ethernet adapter" | find "2" && set "connect=本地连接 2"
ipconfig /all | find "Ethernet adapter" | find "3" && set "connect=本地连接 3"
未测试,只是提供思路,如果想体现得自动化一点,可以把查找条件放到for /l中去,然后用goto跳转。
学习for的用法,你可以没事多翻翻论坛的老贴,全部看明白一遍你就是个批处理的高手了,另外参照我的签名。
Well, first of all, combine the first two sentences:
ipconfig /all | find "Ethernet adapter" >connect.txt
Of course, it can also be used directly in for /f without generating a temporary file.
Second, if you want to get "(Local Area Connection 2)", then after enabling delayed environment variables (setlocal ENABLEDELAYEDEXPANSION), in the do statement of for, assign "Local Area Connection 2:" to the variable connect, and then remove the last colon by outputting!connect:~0,-1!.
Of course, this problem doesn't have to be solved with for. Many times, we should open our minds:
@echo off
set "connect=Local Area Connection"
ipconfig /all | find "Ethernet adapter" | find "2" && set "connect=Local Area Connection 2"
ipconfig /all | find "Ethernet adapter" | find "3" && set "connect=Local Area Connection 3"
Not tested, just providing ideas. If you want to reflect more automation, you can put the search conditions in for /l and then use goto to jump.
To learn the usage of for, you can flip through the old posts in the forum when you have nothing to do. After understanding all of them, you will be a master of batch processing. Also, refer to my signature.