Improving Batch File for loop with start subcommand

前端 未结 3 789
小蘑菇
小蘑菇 2020-12-21 23:40

I\'ve currently got a very simple script which pings 10 IPs:

@ECHO OFF
for /L %%i in (100, 1, 110) DO (
  START /W /B cmd /c ping -n 1 192.168.0.%%i | find \         


        
3条回答
  •  时光取名叫无心
    2020-12-21 23:53

    Seems that I have found a pure batch-file solution. Basically, the script fires several ping commands simultaneously, which all write their result to individual log files; these files are monitored for write-access, because the files are write-locked as long as the respective ping processes are ongoing; as soon as write-access is granted, the first lines of the log files containing the IP addresses are copied into a summary log file. So here is the code -- see all the explanatory rem remarks:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    rem // Predefine IP addresses as an array:
    for /L %%J in (0,1,5) do (
        set "IP[%%J]=127.0.0.%%J"
    )
    
    rem // Ping IP addresses simultaneously, create individual log files:
    for /F "tokens=2,* delims=[=]" %%J in ('set IP[') do (
        start "" /B cmd /C ping -n 2 %%K ^| find "TTL=" ^> "%~dpn0_%%J.log"
    )
    
    rem // Deplete summary log file:
    > "%~dpn0.log" rem/
    
    rem /* Polling loop to check whether individual log files are accessible;
    rem    this works, because a pinging process is not yet finished, the
    rem    respective log file is still opened and therefore write-locked: */
    :POLL
    rem // Give processor some time:
    > nul timeout /T 1 /NOBREAK
    rem /* Loop through all available array elements; for every accomplished
    rem    pinging process, the related array element becomes deleted, so
    rem    finally, there should not be any more array elements defined: */
    for /F "tokens=2 delims=[=]" %%J in ('2^> nul set IP[') do (
        rem // Suppress error message in case log file is write-locked:
        2> nul (
            rem // Try to append nothing to the log file:
            >> "%~dpn0_%%J.log" rem/ && (
                rem /* Appending succeeded, hence log file is no longer locked
                rem    and the respective pinging process has been finished;
                rem    therefore read the first line containing the IP: */
                set "FILE=" & set "IP="
                < "%~dpn0_%%J.log" set /P IP=""
                rem // Copy the read line to the summary log file:
                if defined IP >> "%~dpn0.log" call echo(%%IP%%
                rem // Undefine related array element:
                set "IP[%%J]="
                rem // Store log file path for later deletion:
                set "FILE=%~dpn0_%%J.log"
            )
            rem // Delete individual log file finally:
            if defined FILE call del "%%FILE%%"
        )
    )
    rem // Jump to polling loop in case there are still array elements:
    > nul 2>&1 set IP[ && goto :POLL
    
    endlocal
    exit /B
    

提交回复
热议问题