感谢任何帮助。
一直在尝试编写一个批处理文件,它只是从文本文件中 ping 一个 IP 地址列表。
我知道可以使用以下代码来完成:
FOR /F %i in (yourFile.txt) DO yourcommand %i
并使用该格式创建以下内容:
::@echo off
pause
for /F %%a in (vlans.txt) do (
pause
ECHO Checking connection to subnet %%a, please wait....
PING -n 1 %%a|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :FAIL
:FAIL
ECHO Subnet %%a is unreachable
pause
:SUCCESS
ECHO Subnet %%a has passed!
pause
)
ECHO All subnets have now been tested....................
Pause
它需要一点整理,我已经注释掉了@Echo,这样我就可以看到发生了什么。输出如下:
C:\for /F "tokens=*" %a in (vlans.txt) do (
pause
ECHO Checking connection to subnet %a, please wait....
PING -n 1 %a | find "Reply from " 1>NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :FAIL
ECHO Subnet %a is unreachable
pause
ECHO Subnet %a has passed!
pause
)
C:\>(
pause
ECHO Checking connection to subnet 10.169.169.129, please wait....
PING -n 1 10.169.169.129 | find "Reply from " 1>NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :FAIL
ECHO Subnet 10.169.169.129 is unreachable
pause
ECHO Subnet 10.169.169.129 has passed!
pause
)
Press any key to continue . . .
Checking connection to subnet 10.169.169.129, please wait....
C:\>ECHO Subnet %a has passed!
Subnet %a has passed!
C:\>pause
Press any key to continue . . .
那里发生了很多我不理解或没想到的事情,比如它没有循环遍历文本文件中的每个 IP 地址。它重复自己,首先没有填写变量,然后是变量。此外,在批处理文件运行时,某些变量不会进一步填充 IP 地址。
我是编写批处理文件的新手,所以我知道我的语法是错误的,但不确定我哪里出错了?
vlan.txt 只有一个 IP 地址列表,每行一个。
提前致谢。