|
dosfroum
初级用户
 
积分 154
发帖 54
注册 2005-9-25
状态 离线
|
『楼 主』:
(已结)請問如何抓取文件中的某一行特定文字
使用 LLM 解释/回答一下
請問我下ipconfig > IP.txt
但是我只需要 192.168.1.1 這張網路卡的名字
也就是 IP Address. . . . . . . . . . . . : 192.168.1.1 往上數第4行
要把後面這串文字"Realtek RTL8169/8110 Family Gigabi"設為變數
set CardName=Realtek RTL8169/8110 Family Gigabit
請問要如何做呢 因為卡名會變 但是ip的前面兩組字串不會變 也就是 192.168是固定的 謝謝
Windows 2000 IP Configuration
Host Name . . . . . . . . . . . . : z-h
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Broadcast
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
Ethernet adapter Lan:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Realtek RTL8169/8110 Family Gigabit
Ethernet NIC
Physical Address. . . . . . . . . : 00-0D-61-7A-99-88
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.1.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
DNS Servers . . . . . . . . . . . :
PPP adapter ADSL:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : WAN (PPP/SLIP) Interface
Physical Address. . . . . . . . . : 00-53-45-00-33-11
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 61.230.1.1
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 61.230.1.1
DNS Servers . . . . . . . . . . . : 168.95.1.1
168.95.192.1
Last edited by willsort on 2005-10-27 at 20:09 ]
Please ask me the next ipconfig > IP.txt
But I only need the name of the network card with 192.168.1.1
That is, the 4th line above "IP Address. . . . . . . . . . . . : 192.168.1.1"
Set the following string "Realtek RTL8169/8110 Family Gigabi" as a variable
set CardName=Realtek RTL8169/8110 Family Gigabit
How to do it? Because the card name will change, but the first two strings of the IP will not change, that is, 192.168 is fixed. Thank you
Windows 2000 IP Configuration
Host Name . . . . . . . . . . . . : z-h
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Broadcast
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
Ethernet adapter Lan:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Realtek RTL8169/8110 Family Gigabit
Ethernet NIC
Physical Address. . . . . . . . . : 00-0D-61-7A-99-88
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.1.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
DNS Servers . . . . . . . . . . . :
PPP adapter ADSL:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : WAN (PPP/SLIP) Interface
Physical Address. . . . . . . . . : 00-53-45-00-33-11
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 61.230.1.1
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 61.230.1.1
DNS Servers . . . . . . . . . . . : 168.95.1.1
168.95.192.1
Last edited by willsort on 2005-10-27 at 20:09 ]
|
|
2005-10-26 01:34 |
|
|
JonePeng
金牌会员
      D◎$ Fαп
积分 4562
发帖 1883
注册 2004-1-19 来自 广东广州
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
配合外部命令LMOD可以做到
It can be achieved by cooperating with the external command LMOD
|

----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
|
|
2005-10-26 09:25 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
按照你的思路一步步转化为批处理语言就行了。以下是按照你的思路的批处理实现:
@echo off
setlocal
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr/r /n "\<192.168.1.1\>"') do (
set line=%%a
)
set /a line-=5
echo.%line%
set CardName=
for /f "tokens=1,2,3 delims=:" %%a in ('ipconfig /all ^|findstr/r /n "."') do (
set /a n+=1
if %%a == %line% set CardName=%%c
)
echo.%CardName%
endlocal
关于 set /a line-=5句为什么减 5,实验发现 ipconfig 会在每句后添加换行符,而使其间距倍翻,你可以测定重设其值。
According to your idea, step - by - step conversion into batch language is as follows:
@echo off
setlocal
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr/r /n "\<192.168.1.1\>"') do (
set line=%%a
)
set /a line-=5
echo.%line%
set CardName=
for /f "tokens=1,2,3 delims=:" %%a in ('ipconfig /all ^|findstr/r /n "."') do (
set /a n+=1
if %%a == %line% set CardName=%%c
)
echo.%CardName%
endlocal
Regarding the reason why the set /a line-=5 sentence subtracts 5, experiments find that ipconfig will add line breaks after each sentence, and make its spacing double, and you can measure and reset its value.
|
|
2005-10-26 13:55 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Re JonePeng:
严格意义上说,LMOD并非外部命令,而是第三方工具软件。所以它才不包含在MSDOS6.22/7.10的安装盘中。
Re 无奈何:
并非一定要以192.168.1.1起始来取值的,直接find “Description”然后取第一行的值即可。例:
for /f "delims=: tokens=2" %s in ('ipconfig /all ^| find "Des" ^| find /v "WAN"') do @set CardName=%s
Re JonePeng:
Strictly speaking, LMOD is not an external command, but a third-party tool software. Therefore, it is not included in the installation disks of MSDOS6.22/7.10.
Re 无奈何:
It is not necessary to start taking values from 192.168.1.1. You can directly find "Description" and then take the value of the first line. For example:
for /f "delims=: tokens=2" %s in ('ipconfig /all ^| find "Des" ^| find /v "WAN"') do @set CardName=%s
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-10-26 14:53 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
:to willsort
含有双、多网卡的机器,如此可能会得到非预想的值,我觉得楼主的思路还是稳妥地。
: For machines with dual or multiple network cards, this might get an unexpected value. I think the building - owner's train of thought is still prudent.
|
|
2005-10-26 15:49 |
|
|
dosfroum
初级用户
 
积分 154
发帖 54
注册 2005-9-25
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
Originally posted by willsort at 2005-10-26 14:53:
Re 无奈何:
并非一定要以192.168.1.1起始来取值的,直接find “Description”然后取第一行的值即可。例:
for /f "delims=: tokens=2" %s in ('ipconfig /all ^| find "Des" ^| find /v "WAN"') do @set CardName=%s
版主阿 我之前有提到 為何要用192.168起始 因為一台電腦可能有兩張以上的網卡 但是只有那個ip是固定的 所以..一定要由那行起始下去算 才能得到正確網卡的名稱
請問有何方法修改呢
妳的批次檔可以正確執行 但是 要把 %s 改成 %%s 才對^^"
Last edited by dosfroum on 2005-10-26 at 20:56 ]
Originally posted by willsort at 2005-10-26 14:53:
Re 无奈何:
It's not necessary to start with 192.168.1.1 to get the value. You can directly find "Description" and take the value of the first line. For example:
for /f "delims=: tokens=2" %s in ('ipconfig /all ^| find "Des" ^| find /v "WAN"') do @set CardName=%s
Moderator, I mentioned earlier why we need to start with 192.168. Because a computer may have more than two network cards, but only that IP is fixed. So.. we must start from that line to calculate to get the correct name of the network card.
Is there any way to modify it?
Your batch file can execute correctly, but you need to change %s to %%s^^"
Last edited by dosfroum on 2005-10-26 at 20:56 ]
|
|
2005-10-26 20:11 |
|
|
dosfroum
初级用户
 
积分 154
发帖 54
注册 2005-9-25
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Originally posted by 无奈何 at 2005-10-26 13:55:
按照你的思路一步步转化为批处理语言就行了。以下是按照你的思路的批处理实现:
不會動 不會動 一堆錯誤還沒領悟出來原因 可以大概解釋一下嗎 謝謝
我有很努力的查了 但是看不出端倪來
delims=xxx - 指定分隔符號的集合。 這會取代
預設的空白與定位字元的分隔符號集合。
tokens=x,y,m-n - 指定每一行的哪些文字串應該被
傳到 for 的內部以用來進行每一個重複操作。
這會導致額外的變數名稱被配置。
m-n 格式代表一個範圍,
指定了第 m 個到第 n 個字串項。
如果在 tokens= 字串的最後一個字元是
星號,則會配置一個額外的變數
來接收最後一個字串項被分析後
的其它文字。
findstr /?
在檔案中搜尋字串。
FINDSTR
filename]
/B 如果是在行的開端,則符合類型。
/E 如果是在行的尾端,則符合類型。
/L 逐字使用搜尋字串。
/R 使用搜尋字串為一般表示式。
/S 在現存目錄及所有的子目錄中
搜尋符合的檔案。
/I 指定搜尋不區分大小寫。
/X 列印完全符合的行數。
/V 只列印不含相符字串的行數。
/N 列印每一行符合的行數前的行編號。
/M 只列印包含相符字串的檔案的檔案名稱。
/O 列印每一個相符行之前的字元位移。
/P 略過沒有可列印字元的檔案
/F:file 從指定的檔案讀取檔案清單 (/ 代表主控台)。
/C:string 使用特定的字串作為逐字搜尋的字串。
/G:file 從指定的檔案取得搜尋字串(/ 代表主控台)。
strings 要搜尋的文字。
filename
指定要搜尋的一個或多個檔案。
除非引數的字首有 /C ,否則請以空格將多重搜尋
字串分開。例如,'FINDSTR "hello there" x.y' 將會在檔案 x.y 中搜尋 "hello",或
"there"。'FINDSTR /C:"hello there" x.y' 將會在檔案 x.y 中搜尋
"hello there"。
@echo on
cls
setlocal
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr/r /n "\<192.168.1.2\>"') do (set line=%%a)
set /a line-=3
echo.%line%
set CardName=
for /f "tokens=1,2,3 delims=:" %%a in ('ipconfig /all ^|findstr/r /n "."') do (set /a n+=1 if %%a == %line% set CardName=%%c)
echo. %CardName%
endlocal
E:\>setlocal
E:\>for /F "tokens=1,* delims=:" %a in ('ipconfig /all |findstr/r /n "\<192.168.
1.2\>"') do (set line=%a )
E:\>(set line=16 )
E:\>set /a line-=3
E:\>echo.13
13
E:\>set CardName=
E:\>for /F "tokens=1,2,3 delims=:" %a in ('ipconfig /all |findstr/r /n "."') do
(set /a n+=1 if %a == 13 set CardName=%c )
E:\>(set /a n+=1 if 2 == 13 set CardName= )
遺失運算元。
E:\>(set /a n+=1 if 4 == 13 set CardName= z-h )
遺失運算元。
E:\>(set /a n+=1 if 5 == 13 set CardName= )
遺失運算元。
) >(set /a n+=1 if 6 == 13 set CardName= Broadcast
遺失運算元。
) >(set /a n+=1 if 7 == 13 set CardName= No
遺失運算元。
) >(set /a n+=1 if 8 == 13 set CardName= No
遺失運算元。
) >(set /a n+=1 if 10 == 13 set CardName=
遺失運算元。
E:\>(set /a n+=1 if 12 == 13 set CardName= )
遺失運算元。
E:\>(set /a n+=1 if 13 == 13 set CardName= Realtek RTL8169/8110 Family Gigabit E
thernet NIC )
遺失運算元。
) >(set /a n+=1 if 14 == 13 set CardName= 00-0D-61-7A-88-54
遺失運算元。
) >(set /a n+=1 if 15 == 13 set CardName= No
遺失運算元。
) >(set /a n+=1 if 16 == 13 set CardName= 192.168.1.2
遺失運算元。
) >(set /a n+=1 if 17 == 13 set CardName= 255.255.255.0
遺失運算元。
) >(set /a n+=1 if 18 == 13 set CardName=
遺失運算元。
E:\>(set /a n+=1 if 19 == 13 set CardName= )
遺失運算元。
) >(set /a n+=1 if 21 == 13 set CardName=
遺失運算元。
E:\>(set /a n+=1 if 23 == 13 set CardName= )
遺失運算元。
) >(set /a n+=1 if 24 == 13 set CardName= WAN (PPP/SLIP) Interface
遺失運算元。
) >(set /a n+=1 if 25 == 13 set CardName= 00-53-45-00-00-00
遺失運算元。
) >(set /a n+=1 if 26 == 13 set CardName= No
遺失運算元。
) >(set /a n+=1 if 27 == 13 set CardName= 61.230.197.208
遺失運算元。
) >(set /a n+=1 if 28 == 13 set CardName= 255.255.255.255
遺失運算元。
) >(set /a n+=1 if 29 == 13 set CardName= 61.230.197.208
遺失運算元。
E:\>(set /a n+=1 if 30 == 13 set CardName= 168.95.1.1 )
遺失運算元。
E:\>(set /a n+=1 if 31 == 13 set CardName= )
遺失運算元。
E:\>echo.
E:\>endlocal
E:\>
Last edited by dosfroum on 2005-10-26 at 21:10 ]
Originally posted by Helpless at 2005-10-26 13:55:
Just follow your train of thought and convert it step by step into batch language. The following is the batch implementation according to your train of thought:
Can't move, can't move, a bunch of errors and haven't figured out the reason. Can you explain it roughly? Thanks
I have tried very hard to check, but can't see the clue
delims=xxx - Specify the set of delimiters. This will replace
the default set of delimiters which are whitespace and tab characters.
tokens=x,y,m-n - Specify which strings in each line should be
passed to the internal part of for for each repeated operation.
This will result in additional variable names being allocated.
The m-n format represents a range,
specifying the m-th to n-th string items.
If the last character in the tokens= string is
an asterisk, then an additional variable
will be allocated to receive the other text after
the last string item is analyzed.
findstr /?
Searches for strings in files.
FINDSTR
filename]
/B Matches if at the beginning of a line.
/E Matches if at the end of a line.
/L Uses the search strings literally.
/R Uses the search string as a regular expression.
/S Searches for matching files in the current directory and all subdirectories.
/I Specifies that the search is case-insensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain the matching string.
/N Prints the line number before each line that matches.
/M Prints only the file names of files that contain the matching string.
/O Prints the character offset before each matching line.
/P Skips files with non-printable characters.
/F:file Reads file list from the specified file (/ represents the console).
/C:string Uses the specific string as a literal search string.
/G:file Gets search strings from the specified file (/ represents the console).
strings The text to search for.
filename
Specifies one or more files to search.
Unless the prefix of the argument is /C, separate multiple search
strings with spaces. For example, 'FINDSTR "hello there" x.y' will search for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' will search for
"hello there" in file x.y.
@echo on
cls
setlocal
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr/r /n "\<192.168.1.2\>"') do (set line=%%a)
set /a line-=3
echo.%line%
set CardName=
for /f "tokens=1,2,3 delims=:" %%a in ('ipconfig /all ^|findstr/r /n "."') do (set /a n+=1 if %%a == %line% set CardName=%%c)
echo. %CardName%
endlocal
E:\>setlocal
E:\>for /F "tokens=1,* delims=:" %a in ('ipconfig /all ^|findstr/r /n "\<192.168.
1.2\>"') do (set line=%a )
E:\>(set line=16 )
E:\>set /a line-=3
E:\>echo.13
13
E:\>set CardName=
E:\>for /F "tokens=1,2,3 delims=:" %a in ('ipconfig /all ^|findstr/r /n "."') do
(set /a n+=1 if %a == 13 set CardName=%c )
E:\>(set /a n+=1 if 2 == 13 set CardName= )
Missing operand.
E:\>(set /a n+=1 if 4 == 13 set CardName= z-h )
Missing operand.
E:\>(set /a n+=1 if 5 == 13 set CardName= )
Missing operand.
) >(set /a n+=1 if 6 == 13 set CardName= Broadcast
Missing operand.
) >(set /a n+=1 if 7 == 13 set CardName= No
Missing operand.
) >(set /a n+=1 if 8 == 13 set CardName= No
Missing operand.
) >(set /a n+=1 if 10 == 13 set CardName=
Missing operand.
E:\>(set /a n+=1 if 12 == 13 set CardName= )
Missing operand.
E:\>(set /a n+=1 if 13 == 13 set CardName= Realtek RTL8169/8110 Family Gigabit E
thernet NIC )
Missing operand.
) >(set /a n+=1 if 14 == 13 set CardName= 00-0D-61-7A-88-54
Missing operand.
) >(set /a n+=1 if 15 == 13 set CardName= No
Missing operand.
) >(set /a n+=1 if 16 == 13 set CardName= 192.168.1.2
Missing operand.
) >(set /a n+=1 if 17 == 13 set CardName= 255.255.255.0
Missing operand.
) >(set /a n+=1 if 18 == 13 set CardName=
Missing operand.
E:\>(set /a n+=1 if 19 == 13 set CardName= )
Missing operand.
) >(set /a n+=1 if 21 == 13 set CardName=
Missing operand.
E:\>(set /a n+=1 if 23 == 13 set CardName= )
Missing operand.
) >(set /a n+=1 if 24 == 13 set CardName= WAN (PPP/SLIP) Interface
Missing operand.
) >(set /a n+=1 if 25 == 13 set CardName= 00-53-45-00-00-00
Missing operand.
) >(set /a n+=1 if 26 == 13 set CardName= No
Missing operand.
) >(set /a n+=1 if 27 == 13 set CardName= 61.230.197.208
Missing operand.
) >(set /a n+=1 if 28 == 13 set CardName= 255.255.255.255
Missing operand.
) >(set /a n+=1 if 29 == 13 set CardName= 61.230.197.208
Missing operand.
E:\>(set /a n+=1 if 30 == 13 set CardName= 168.95.1.1 )
Missing operand.
E:\>(set /a n+=1 if 31 == 13 set CardName= )
Missing operand.
E:\>echo.
E:\>endlocal
E:\>
Last edited by dosfroum on 2005-10-26 at 21:10 ]
|
|
2005-10-26 20:28 |
|
|
dosfroum
初级用户
 
积分 154
发帖 54
注册 2005-9-25
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
搞定 原來是 () 的位置影響很大
@echo off
cls
setlocal
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr/r /n "\<192.168\>"') do (set line=%%a)
set /a line-=3
echo.%line%
set CardName=
for /f "tokens=1,2,3 delims=:" %%a in ('ipconfig /all ^|findstr/r /n "."') do (
set /A n+=1
if %%a == %line% set CardName=%%c
)
echo. %CardName%
endlocal
續問 可法幫忙解釋
delims=xxx tokens=x,y,m-n 這兩個參數 集%%c代表意思
因為我想要試著顯示出該行 IP 但是都顯示:以前的東西 請幫忙解惑
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr /r "192.168"') do (set IPaddr=%%a)
echo. %IPaddr%
結果只顯示這樣 @@a
IP Address. . . . . . . . . . . .
Got it. The position of () has a big impact
@echo off
cls
setlocal
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr/r /n "\<192.168\>"') do (set line=%%a)
set /a line-=3
echo.%line%
set CardName=
for /f "tokens=1,2,3 delims=:" %%a in ('ipconfig /all ^|findstr/r /n "."') do (
set /A n+=1
if %%a == %line% set CardName=%%c
)
echo. %CardName%
endlocal
Follow-up question: Can you help explain
delims=xxx tokens=x,y,m-n these two parameters, what does %%c represent
Because I want to try to display the IP of that line but it always shows the previous things, please help solve the confusion
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr /r "192.168"') do (set IPaddr=%%a)
echo. %IPaddr%
The result only shows like this @@a
IP Address. . . . . . . . . . . .
|
|
2005-10-26 21:31 |
|
|
dosfroum
初级用户
 
积分 154
发帖 54
注册 2005-9-25
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
總合整理
setlocal,endlocal <<< 代表什麼意思??
delims=: 表示已冒號做區分
tokens=2 表示用冒號區分成兩段後 取後面那段
tokens=1,2,3 <<< 代表什麼意思??
%%c <<<<<<這個是從哪來的
抓出來的卡名有多一個空白
我的解決方式是
set IA=%IPaddr:~1%
請問 有告好的方法嗎
Last edited by dosfroum on 2005-10-26 at 21:56 ]
Summary arrangement
What does setlocal, endlocal <<< mean??
delims=: means to separate with colons
tokens=2 means that after separating into two segments with colons, take the latter segment
What does tokens=1,2,3 <<< mean??
Where does %%c <<<<<< come from
There is an extra space in the captured card name
My solution is
set IA=%IPaddr:~1%
Is there a better way?
Last edited by dosfroum on 2005-10-26 at 21:56 ]
|
|
2005-10-26 21:44 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
1、setlocal,endlocal 表示在其区间内使用自己的环境变量。
2、tokens=1,2,3 delims=: %%a ,表示获取以 : 分割的前3个字符串,分别付值给 %%a,%%b,%%c,后两个是系统自动分配的。
3、多出的空白字符,像你这样取子字符串,排除掉就可以了。
其实你产生问题联机帮助里都有说明,for /? 将显示的内容反复读几遍,for是很强大的命令值得花点功夫学习。
1、setlocal and endlocal indicate using their own environment variables within the interval.
2、tokens=1,2,3 delims=: %%a means to get the first 3 strings split by : and assign them to %%a, %%b, %%c respectively, and the last two are automatically assigned by the system.
3、For the extra blank characters, just exclude them when taking the substring like you do.
Actually, the help for the problem is all explained in the online help. Read the content displayed by for /? several times. The for command is a very powerful command and it's worth taking some time to learn.
|
|
2005-10-26 23:25 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Re 无奈何 & dosfroum:
双网卡的情况确实会有问题,多网卡的情形应该很难碰到吧?不过多个网络连接倒是很可能的,所以需要再做一些变化。但是192.168的网卡能说明什么问题呢?是说它是本地连接的网卡(局域网卡)吗?我的本地网卡是10.0起首的。
我在4楼写的例子是在命令行运行的,而非是批处理,从set前面的@就应该可以看出来;如果是批处理,我会让它结构上更完整和严谨一些,并且加上[code]和[/code]的代码修饰符。
至于 无奈何 兄的代码,仍然可以再简化一下,不用取行号,只用一个FOR即可将之析出。思路如下:分析IPCONFIG的每行输出,如果遇到Description...则取后面的网卡名,如果遇到192.168...则跳出循环,具体代码就不提供了。
去除串前空白,用set是最简单的;取IP的那句是因为tokens的问题,应该是tokens=2,这样%%a才会被赋予第2个“令牌”(使用delims切分出来的字符串子串)的值,你的用法使IP地址赋给了%%b。
至于FOR的中文联机帮助,我建议只看前半部分,后半部分明显是未经校正过的机器翻译结果,不仅语法晦涩难懂,而且给出的usebackq的例句是错误的,不如不看。当然如果你的英文水平可以,可以chcp 437后浏览其英文版。
FOR是很强大,也因此有很多BUG,我的建议是尽量少用usebackq,否则很容易碰到问题。而且我觉得,完全没有必要把许多功能都集中的FOR一个命令上的,这样只会带来命令的复杂性提高和健壮性下降,如果把它们按照功能分类拆分为几个单独的命令,那就既利于用户学习,也利于编程实现。
Re 无奈何 & dosfroum:
The situation with dual network cards does indeed have problems. It should be very difficult to encounter multiple network cards? But multiple network connections are very likely, so some changes need to be made. But what does the 192.168 network card indicate? Does it mean it's the local connection network card (LAN card)? My local network card starts with 10.0.
The example I wrote on the 4th floor is run in the command line, not a batch script. You can tell from the @ before set; if it were a batch script, I would make it more complete and rigorous in structure and add and
code modifiers.
As for Brother 无奈何's code, it can still be simplified. There's no need to get the line number; just use one FOR to extract it. The idea is as follows: Analyze each line of the IPCONFIG output. If Description... is encountered, take the subsequent network card name. If 192.168... is encountered, jump out of the loop. The specific code won't be provided.
Removing leading whitespace is simplest with set; the line to get the IP is because of the tokens issue. It should be tokens=2, so %%a will be assigned the value of the 2nd "token" (the substring of the string split by delims). Your usage assigned the IP address to %%b.
As for the Chinese online help for FOR, I suggest only looking at the first half. The second half is obviously a machine translation result that hasn't been corrected. It's not only grammatically obscure but also the example for usebackq is incorrect, so it's better not to look at it. Of course, if your English level is okay, you can chcp 437 and browse the English version.
FOR is very powerful, and therefore has many bugs. My suggestion is to use usebackq as little as possible, otherwise problems are easy to encounter. Moreover, I think there's no need to concentrate many functions in one FOR command. This will only increase the complexity and reduce the robustness of the command. If they are split into several separate commands according to functions, it's beneficial for users to learn and for programming implementation.
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-10-27 01:28 |
|
|
dosfroum
初级用户
 
积分 154
发帖 54
注册 2005-9-25
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
是有答案了 可是我可不可以續問
willsort版主 妳提到的思路 我想了一下午了 沒頭緒
那我一上午是在做啥呢?想另一個問題 因為某些電腦的網卡會抓到 #2 #3 ....
但是我只要前面那串字阿 不要#2 我有想過用 set 扣掉後面的數字 但是如果遇到沒有#2的狀態 卡名就會被截斷
附帶192.168的網路卡 並不能說明什麼 只是這個是我們設定的規則之一 所以剛好可以由這個字串來找尋
There is an answer, but can I continue to ask?
Moderator willsort, the idea you mentioned, I have thought about it for an afternoon, and I have no clue.
Then what was I doing for the whole morning? Thinking about another problem. Because the network card of some computers will catch #2 #3....
But I just want the first string of characters. Don't want #2. I thought of using set to deduct the subsequent numbers, but if there is no #2 state, the card name will be truncated.
Attached with the 192.168 network card, it can't explain anything. It's just one of the rules we set, so we can just find it from this string of characters.
|
|
2005-10-28 00:31 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Re dosfroum:
那试试这个程序吧
:: GetCard.cmd - Get "Description" of net card that IP address is "192.168.*.*"
:: Will Sort - 2005-10-28 - CMD@WinXP
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%s in ('ipconfig /all') do (
set line=%%s
echo.!line! | findstr "Description" >nul && set CardName=!line:~44!
echo.!CardName! | findstr /r "\<#" >nul && set CardName=!CardName:~0,-3!
echo.!line! | findstr "192.168." >nul && goto EchoName
)
:EchoName
echo CardName="%CardName%"
Re: dosfroum:
Then try this program:
:: GetCard.cmd - 获取IP地址为“192.168.*.*”的网卡的“描述”
:: Will Sort - 2005-10-28 - CMD@WinXP
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%s in ('ipconfig /all') do (
set line=%%s
echo.!line! | findstr "Description" >nul && set CardName=!line:~44!
echo.!CardName! | findstr /r "\<#" >nul && set CardName=!CardName:~0,-3!
echo.!line! | findstr "192.168." >nul && goto EchoName
)
:EchoName
echo CardName="%CardName%"
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-10-28 10:53 |
|