I don't understand why you want to ping the entire IPv4 address range, i.e. ping all IPv4 addresses of the world. Do you know that you try to ping 4 294 967 296 addresses?
The line set %%A.%%B.%%C.%%D = IP
is completely wrong. The name of the variable and the string value to assign to the variable are on wrong side of the equal sign. And take at look on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation why there should be no spaces around the equal sign.
Delayed environment variable expansion is needed whenever a variable is defined or modified within a command block starting with (
and ending with matching )
and the value of the variable is referenced in same command block. Run in a command prompt window set /?
and read all output help pages for details about delayed expansion as used in the code below.
Windows command processor does not support labels within a FOR loop. The solution here is using IF with ELSE.
The IF condition could be never true as compared is a string with no double quotes with a string with double quotes. The surrounding double quotes are needed here for the string comparison, but they are not removed by command IF from the compared strings.
The IF condition for string comparison is also the reason for the syntax error because environment variable iptemp
is not defined above first FOR and is therefore undefined on parsing the command block. This results in replacing %iptemp%
already before most outer FOR being first time executed by nothing resulting in the line
if == "Packets: Sent = 1, Received = 0, Lost = 1 (100 loss)," (
And this line is of course completely wrong. Note: Single percent sign is also removed.
The percent sign in the string to compare must be escaped with one more percent sign to be interpreted as literal character and not as beginning of a variable reference.
In code below in last ECHO line the exclamation mark must be escaped with ^^
to be interpreted as literal character because of delayed expansion being enabled which changes meaning of !
from a simple literal character to delimiter of a variable name whose value should be referenced without expansion on parsing the command block.
@echo off
setlocal EnableDelayedExpansion
color b
title Get Online IPs
set "ListFile=IPlist.txt"
if exist "%ListFile%" del "%ListFile%"
for /L %%A in (0,1,255) do (
for /L %%B in (0,1,255) do (
for /L %%C in (0,1,255) do (
for /L %%D in (0,1,255) do (
set "IP=%%A.%%B.%%C.%%D"
for /F "delims=" %%i in ('%SystemRoot%\System32\ping.exe -n 1 !IP!') do set "iptemp=%%i"
if "!iptemp!" == "Packets: Sent = 1, Received = 0, Lost = 1 (100%% loss)," (
echo !IP! is offline.
) else (
echo !IP! is online^^!>>"%ListFile%"
)
)
)
)
)
endlocal
On my German Windows the string comparison is never true and therefore all IPs are "online" which of course is wrong.
Much better would be using exit code of console application PING which is 0 on successful pinging and 1 on error. The exit code of previously executed command or console application can be evaluated with if errorlevel X
respectively if not errorlevel X
depending on checking exit code (errorlevel) for greater or equal X
or lower than X
.
@echo off
setlocal EnableDelayedExpansion
color b
title Get Online IPs
set "ListFile=IPlist.txt"
if exist "%ListFile%" del "%ListFile%"
for /L %%A in (0,1,255) do (
for /L %%B in (0,1,255) do (
for /L %%C in (0,1,255) do (
for /L %%D in (0,1,255) do (
set "IP=%%A.%%B.%%C.%%D"
%SystemRoot%\System32\ping.exe -n 1 !IP! >nul
if errorlevel 1 (
echo !IP! is offline.
) else (
echo !IP! is online^^!>>"%ListFile%"
)
)
)
)
)
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
color /?
del /?
echo /?
endlocal /?
for /?
if /?
ping /?
set /?
setlocal /?
title /?
Read also the Microsoft articles: