这是我正在努力的事情:
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
)
)
我遇到的问题是,当变量%count_title%通过脚本计数时,函数TITLE没有得到更新。
set /a title_count+=1
不起作用
显示为:
Scanning for online computers 0 / 5
可以告诉我在这里做错了吗?
提前致谢。
幻觉
您好,
我按照建议的方式尝试过: 当使用最后一个GOTO:EOF时,它完成了脚本的其余部分 IT对我来说没有意义,如果我删除最后一个GOTO:eof,只有workstation.txt中的第一行被处理/解析。
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=%1 && call includes\what.bat %color_fail% && echo %1 >> logs\unreachable.txt && echo %1, offline, %date%, %time% >> logs\offline.txt
)
goto :eof
)
答案 0 :(得分:3)
请阅读:Environment variable expansion occurs when the command is read。
突出点:
for
命令(括在括号中的整个主体)时,您的变量会被扩展。!VARNAME!
代替%VARNAME%
来避免它。setlocal EnableExtensions EnableDelayedExpansion
。另外,请确保在goto :EOF
之后有转到(例如call :next
),因为发布的代码会在next
一段时间后再运行。
答案 1 :(得分:0)
您可以使用setlocal EnableDelayedExpansion
并将%
语法更改为!
语法,解决在该循环内初始化的循环内的变量,就像 atzz < / em>建议。
但是有一种不同的方法。您只需将循环体移动到(另一个)子程序即可。这样变量就会按预期扩展。
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 )