Batch file 尝试使用ping使批处理文件处理更快。现在跳出循环

Batch file 尝试使用ping使批处理文件处理更快。现在跳出循环,batch-file,command-line,Batch File,Command Line,下面的脚本可以正常工作,但在运行脱机主机时速度会大大减慢 @echo off rem Setup the output file. This just wipes the pre-existing file from last set of data mining for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b) for /f "tokens=1-2 delims=/:" %%a in ('t

下面的脚本可以正常工作,但在运行脱机主机时速度会大大减慢

@echo off
rem Setup the output file. This just wipes the pre-existing file from last set of data mining
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
echo %mydate%_%mytime% > output\datamine.txt
**setlocal enabledelayedexpansion
for /f %%a in (hosts.txt) do (
psexec \\\%%a findstr /I ":" "C:\ProgramData\Firewall\event.log" >> output\datamine.txt )
endlocal**
我用ping语句重写,打算跳过没有应答的主机。下面是它的要点:

(hosts.txt)do(
ping-n 1-w 100%%a | for/f“skip=1 tokens=3 delims=“%%b in('findstr bytes')do(设置COMPNAME=%%b) 如果错误级别1转到失败 psexec\\%%%a findstr/I:“C:\ProgramData\Firewall\event.log”>>output\datamine.txt) 转到终点 :失败 回声主机没有应答 :结束 端部 问题很明显,当主机无法应答ping时,我正在退出do循环。我需要脚本来查看errorlevel 1,然后直接跳到下一个主机。不确定循环应该结束在哪里。此外,我不太确定我选择的“goto”行。
有什么建议吗?

您正在退出for循环。不要执行“转到”操作,而是添加一个else并在其后移动括号

下面是我编写的一个函数,用于实现这一点,以及如何实现它

for /f %%a in (hosts.txt) do (
   call :IsPingable %%a && (
   psexec \\%%a findstr /I ":" "C:\ProgramData\Firewall\event.log" >> output\datamine.txt 
   ) || (
     echo host %%a not answering
   )
)   
exit /b

:IsPingable comp
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
示例代码:

for /f %%a in (hosts.txt) do ping -n 1 "%%a">nul && call :success "%%a"|| call :fail "%%a"
goto:eof

:success
echo success: %~1
goto:eof

:fail
echo fail: %~1
goto:eof

就这样。好多了。我不知道为什么我没有想到这个电话。非常感谢。
for /f %%a in (hosts.txt) do ping -n 1 "%%a">nul && call :success "%%a"|| call :fail "%%a"
goto:eof

:success
echo success: %~1
goto:eof

:fail
echo fail: %~1
goto:eof