开发者

FOR /F issue running from a text file + counting up value

Here is something I'm struggeling with:

Title Scanning online computers: && set /a title_count=0
call :next


:next
FOR /F "tokens=1" %%a IN (workstation.txt) do (
title Scanning for online computers: %title_count% / %workstation%
ping -n 1 %%a | find "bytes=" >nul
set /a title_count=title_count+=1

if NOT ERRORLEVEL 1 (
set color=%%a && call includes\what.bat %color_pass% && echo %%a >> logs\reachable.txt
) else ( 
set color=%%a && call includes\what.bat %color_fail% && echo %%a >> logs\unreachable.txt && echo %%a, offline, %     date%, %time% >> logs\offline.txt
)
)

The problem I have here is that the function TITLE is not getting updated while the variable %count_title% is counting up through the script.

set /a title_count+=1 

doesn't work either

It's displayed as:

 Scanning for online computers 0 / 5

Can sombody tell me what I'm doing wrong here?

Thanks in advance.

Illusion

Hi,

I've tried it the way as suggested: It finishes the rest of the script when using the last GOTO :EOF IT doesn't make sense to me, if I remove the last GOTO :eof, only the first row in workstation.txt is getting processed/parsed.

       Scanning online computers: && set /a title_count+=1`
       call :next
       ::added as possibly missing
       GOTO :EOF

       :next
        FOR /F "tokens=1" %%a IN (workstation.txt) DO CALL :pingstation %%a
        GOTO :EOF

        :pingstation
        title Scanning for online computers: %title_count% / %workstation%
        ping -n 1 %1 | find "bytes=" >nul
        set /a title_count+=1

        if NOT ERRORLEVEL 1 (
        set color=%1 && call includes\what.bat %color_pass% && echo %1 >> logs\reachable.txt
        ) else ( 
         set color=开发者_StackOverflow中文版%1 && call includes\what.bat %color_fail% && echo %1 >> logs\unreachable.txt && echo %1, offline, %date%, %time% >> logs\offline.txt
        )

        goto :eof

        )


Read this: Environment variable expansion occurs when the command is read.

Salient points:

  • Your variables are expanded right when for command (and its entire body enclosed in parentheses) is parsed.
  • Use !VARNAME! instead of %VARNAME% to avoid it.
  • For better portability across OS versions/setups, it's a good idea to stick a setlocal EnableExtensions EnableDelayedExpansion at the beginning of your batch file.

Also, make sure there is a goto (e.g., goto :EOF) after call :next, because the code as posted will run through next one extra time.


You can go with setlocal EnableDelayedExpansion and changing the % syntax to the ! one when addressing vars inside the loop that are initialised within that very loop, just as atzz has suggested.

But there's a different approach. You can simply move the body of the loop to a(nother) subroutine. That way the variables would expand as expected.

Title Scanning online computers: && set /a title_count=0
call :next
::added as possibly missing
GOTO :EOF

:next
FOR /F "tokens=1" %%a IN (workstation.txt) DO CALL :pingstation %%a
GOTO :EOF

:pingstation
title Scanning for online computers: %title_count% / %workstation%
ping -n 1 %1 | find "bytes=" >nul
set /a title_count+=1

if NOT ERRORLEVEL 1 (
set color=%1 && call includes\what.bat %color_pass% && echo %1 >> logs\reachable.txt
) else ( 
set color=%1 && call includes\what.bat %color_fail% && echo %1 >> logs\unreachable.txt && echo %1, offline, %date%, %time% >> logs\offline.txt
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜