通过批处理脚本迭代文本文件而不使用for语句

时间:2016-10-26 14:08:32

标签: batch-file

我需要一种方法来不使用for语句,或者在条件满足时找到一种方法来摆脱循环过程。

这就是我现在所拥有的......

for /f "delims=" %%a in ('type pclist.txt') do (
  ping -n 1 %%a | findstr /i "reply" >nul 2>nul
  if %errorlevel% equ 0 (
    if exist "\\%%a\c$\windows\temp\installed.txt" (
      echo %%a - Already installed >>results.txt
    )
    if not exist "\\%%a\c$\windows\temp\installed.txt" (
      echo %%a - Not installed >>results.txt
    )
  )
  ping -n 1 %%a | findstr /i "timed" >nul 2>nul
  if %errorlevel% equ 0 echo %%a - No PING response >>results.txt
  ping -n 1 %%a | findstr /i "transmit" >nul 2>nul
  if %errorlevel% equ 0 echo %%a - PING transmit failed >>results.txt
)

这给了我这样的结果......

192.168.144.1 - Already installed
192.168.144.1 - No PING response
192.168.144.1 - PING transmit failure
192 168.144.2 - Already installed
192.168.144.2 - No PING response
192.168.144.2 - PING transmit failure
192.168.144.3 - Not installed
192.168.144.3 - No PING response
192.168.144.3 - PING transmit failure

我想做的是在满足第一个条件时退出for循环。所以,我会有这样的结果......

192.168.144.1 - Already installed
192 168.144.2 - Already installed
192.168.144.3 - Not installed

我希望我只是简单地忽略了一些简单的东西,这对我来说只是另一个I-D-10-T错误。 (LOL)

提前完成。

用简单的英语。这是目标:

Using FOR command, read list of IP addresses from text file.
  For each IP address read, PING and check response.
    If PING returns a reply, check for existence of file.
      If file exists,
        ECHO value 1 to results file, end FOR loop, proceed with next IP address in list.
      If file does not exist,
        ECHO value 2 to results file, end FOR loop, proceed with next IP address in list.
    If PING does not return a reply, ECHO PING error text to results file, end FOR loop, proceed with next IP address in list.

我希望这更容易理解。

再次谢谢。

2 个答案:

答案 0 :(得分:1)

为了避免嵌套的IF / Else代码块,您应该使用带调用的subs。重定向到file_names = ['foo.txt', 'bar.txt'] def get_string(file_name): with open(file_name, 'r') as fh: contents = fh.read() return contents strings = [get_string(f) for f in file_names] 时,使用C:\windows\temp也可能会出现问题。 TTL是ping的更好指标 - 取决于区域设置。

C:\Users\%USERNAME%\AppData\Local\Temp

返回此输出

@Echo off
Set Res=^>^>Results.txt
Echo %date% %time% %Res:~1%
Set Inst=Windows\temp\installed.txt
for /f "delims=" %%a in ('type pclist.txt') do Call :TestPC %%a
Type %Res:~2%
Goto :Eof

:TestPC
ping -n 1 -w 500 %1|findstr "TTL">nul 2>&1||(Call :Check %1 &Goto :Eof)
:: A rights Problem might exist
if     exist "\\%1\c$\%Inst%" echo %1 - Already installed %Res%
if not exist "\\%1\c$\%Inst%" echo %1 - Not installed %Res%
Goto :Eof

:Check
ping -n 1 -w 500 %1 | findstr /i "timed" >nul 2>nul
if %errorlevel% equ 0 %Res% echo %1 - No PING response
ping -n 1 -w 500 %1 | findstr /i "transmit" >nul 2>nul
if %errorlevel% equ 0 %Res% echo %1 - PING transmit failed
Goto :Eof

答案 1 :(得分:0)

  if %errorlevel% equ 0 (

(多次使用) - %errorlevel%指的是在解析FOR时<{1}} 的值,而不是在运行时。你需要替换

errorlevel

在每种情况下提供所需的功能(如果运行时错误级别不是(1或更大))

因此,你&#34;采样电流输出&#34;是假的 - 你不能使用你所显示的代码得到的输出if not errorlevel 1 不会随着每个循环而改变。此外,您的样本显示&#34;失败&#34;而你的代码指定&#34;失败&#34;。

%errorlevel%你是什么意思?您可以通过在whatever first condition is met语句关闭后删除或注释掉4行来获得您的建议输出。