The batch file could have following command lines:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "LOGFILE=%~dp0IPScript.log"
set "IPLIST=%~dp0IPLIST.txt"
set "AddressCount=0"
echo Script started>"%LOGFILE%"
for /F "delims==" %%I in ('set IP_Address_ 2^>nul') do set "%%I="
if exist "%IPLIST%" for /F "useback delims=" %%I in ("%IPLIST%") do (
set /A AddressCount+=1
call set "IP_Address_%%AddressCount%%=%%I"
)
if not %AddressCount% == 0 (
if %AddressCount% == 1 (
echo The IP address is:
) else echo The IP addresses are:
echo(
set IP_Address_
) >>"%LOGFILE%"
endlocal
The batch file first two command line define the execution environment which means:
- Disable command echo mode.
- Push current command extension state on stack and enable command extensions.
- Push current delayed expansion state on stack and disable delayed environment variable expansion.
- Push path of current directory on stack.
- Push pointer to current list of environment variables on stack and create a copy of the entire current environment variables list to use next.
The third and fourth line define two environment variables with the name of the log file and the name of the IP address list file with full qualified file name. The file path of both files is defined as path of the directory containing the batch file referenced with %~dp0
. This path always ends with \
and for that reason no additional backslash is needed on concatenating this path with the two file names.
The fifth line define the environment variable AddressCount
with value 0
.
The sixth line creates the log file in current directory with overwriting an already existing log file. There is no space left to redirection operator >
as this space would be output by command ECHO and therefore written as trailing space also into the log file.
The first FOR command with option /F
starts in background with %ComSpec% /c
one more command process with the command line between '
appended as additional arguments. So executed is in background with Windows installed into C:\Windows
:
C:\Windows\System32\cmd.exe /c set IP_Address_ 2>nul
Windows creates a copy of current list of environment variables for the command process started in background. The background command process runs command SET to output all environment variables with name, an equal sign and the string value assigned to the variable line by line of which name starts with IP_Address_
. This output to handle STDOUT of background command process is captured by FOR respectively the command process which is processing the batch file. The error message output by SET on no environment variable define with a name starting with IP_Address_
is redirected from handle STDERR to device NUL to suppress this error message.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir
command line with using a separate command process started in background.
FOR processes the captured output line by line after started background command process closed itself after execution of command SET. Empty lines are always ignored by FOR which can be ignored as there are no empty lines output by SET.
FOR would split up by default the current line into substrings using normal space and horizontal tab as delimiters. This default line splitting behavior is not wanted here. The option delims==
defines the equal sign as string delimiter to split the line on =
which is the character between variable name and variable value.
FOR would next ignore the line if the first substring would start with a semicolon which is the default end of line character. The command SET outputs only lines starting with IP_Address_
and for that reason the default eol=;
can be kept in this case.
FOR assigns just the first substring to the specified loop variable I
as tokens=1
is the default. That is exactly the wanted behavior in this case.
So FOR assigns one environment variable name starting with IP_Address_
to loop variable I
and runs next the command SET to delete this environment variable in current list of environment variables of command process processing the batch file.
In other words the first FOR is for deletion of all environment variables of which name starts with IP_Address_
defined by chance outside the batch file.
The next line first checks if the file with the list of environment variables exists at all in directory of the batch file. In this case once again FOR is used to process lines, but this time read line by line from the specified list file instead of captured output of a background command process. The usage of "
instead of '
with the option usebackq
makes the difference.
There is used the option delims=
to define an empty list of delimiters resulting in getting each non-empty line not starting with ;
assigned completely to the specified loop variable I
.
For each string assigned to loop variable I
the current value of environment variable AddressCount
is incremented by one using an arithmetic expression evaluated by command SET.
This value is used on next command line to define an environment variable of which name starts with IP_Address_
and has appended the current address count value with line read from file assigned to the environment variable.
There is usually used delayed expansion for such tasks on which the second command line in command block of second FOR loop would be:
set "IP_Address_!AddressCount!=%%I"
But the code above uses the alternative method with command call
to parse set "IP_Address_%%AddressCount%%=%%I"
a second time which was already modified to set "IP_Address_%AddressCount%=%I"
before the IF condition left to FOR was executed at all.
The next IF condition checks if any line was read from the list file with the IP addresses. In this case first an information line is output depending on having read exactly one line from the file or more than one line. Then an empty line is output and last all environment variables of which name starts with IP_Address_
with =
and the line (IP address) assigned to the environment variable. All this output is appended to the log file.
The last command restores previous execution environment which means:
- Discard the current list of environment variables and pop from stack the pointer to initial list of environment variables resulting in restoring the initial list of environment variables. In other words all environment variables defined or modified by the batch file after command SETLOCAL in second command line are lost forever.
- Pop path of current directory from stack and make this directory again the current directory. The current directory between
setlocal
and endlocal
was not changed by the code between and so this does not matter here.
- Pop delayed expansion state from stack and enable or disable delayed environment variable expansion accordingly to restore initial delayed expansion behavior.
- Pop current command extension state from stack and enable or disable command extensions accordingly to restore initial command extension behavior.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
echo /?
endlocal /?
for /?
if /?
set /?
setlocal /?
See also: