Batch file ping多台计算机的IF语句

Batch file ping多台计算机的IF语句,batch-file,if-statement,command,ping,Batch File,If Statement,Command,Ping,我正在尝试创建一个小的批处理文件,用于检查从文本文件读取的多台PC。对于它发现的任何可ping的PC,它会在“结果”文本文件中写一行这样的话。以下是我得到的: @Echo off set file=C:\logs\registercheck.txt date /t >%file% FOR /F %%I IN (C:\work\regnames.txt) DO (ping /n 1 %%I | ping /n 1 %%I | IF errorlevel 1 goto :nextreg | e

我正在尝试创建一个小的批处理文件,用于检查从文本文件读取的多台PC。对于它发现的任何可ping的PC,它会在“结果”文本文件中写一行这样的话。以下是我得到的:

@Echo off
set file=C:\logs\registercheck.txt
date /t >%file%
FOR /F %%I IN (C:\work\regnames.txt) DO (ping /n 1 %%I | ping /n 1 %%I | IF errorlevel 1 goto :nextreg | echo %%I is still on and has not been powered off! >>%file% | :nextreg)
PAUSE  
所以…当我运行该文件时,我得到了多行“goto此时出乎意料”,并且在我的输出文本文件中唯一写入的是日期。我做错了什么

谢谢大家!

@Echo off
    setlocal enableextensions disabledelayedexpansion

    set "logFile=C:\logs\registercheck.txt"
    set "inputFile=C:\work\regnames.txt"

    >>"%logFile%" date /t

    for /f "usebackq delims=" %%i in ("%inputFile%") do (
        ping -n 1 %%i >nul 2>nul 
        if not errorlevel 1 (
            >>"%logFile%" echo(%%i is still on and has not been powered off! 
        )
    )
你有两个错误

第一个是,要将所有命令放在一行中,分隔符不是管道字符(
),而是符号(
&

第二个是在
for
命令的
do
代码块内,如果执行了一个
goto
,则
for
命令完成,与标签放置的位置无关。而
代码块的
内的标签通常会产生错误(取决于其位置)

如果您想要一个单行循环,而不是前面的代码,则可以将其编写为

for /f "usebackq delims=" %%i in ("%inputFile%") do ( ping -n 1 %%i >nul 2>nul & if not errorlevel 1 >>"%logFile%" echo(%%i is still on and has not been powered off! )

它使用
&&
构造。如果不是错误级别1…
,它将用作
的快捷方式。如果
&&
左侧的命令未引发错误级别,则执行右侧的命令

这适用于批量sintax。现在是ping。ping命令的行为方式因ip版本而异。ping-an ipv4地址与ping-an ipv6地址不同。如果需要,您可以从子线程抓取来处理差异

for /f "usebackq delims=" %%i in ("%inputFile%") do ( ping -n 1 %%i >nul 2>nul && >>"%logFile%" echo(%%i is still on and has not been powered off! )