The first mistake is in line:
SET str1 = " Connect failed"
This line defines an environment variable with name str1
with a space at end of name with the value " Connect failed"
assigned to it. The leading space and the two double quotes are also assigned to the variable as part of the string.
As the answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explains in detail, the right syntax would be:
set "str1=Connect failed"
This command line defines an environment variable str1
with the value Connect failed
assigned to it.
Run in a command prompt window set /?
to get displayed the help for this command on several display pages.
The second mistake is in line:
if \i %str1%==%%n echo "true"
Options/switches are on Windows specified with /
and \
is used as directory separator. So the switch for case-insensitive comparison must be /i
and not \i
.
Run in a command prompt window if /?
for help on IF command.
The third mistake is the attempt to define an environment variable within a command block with assigning a string value to the environment variable and reference the value of this environment variable not using delayed expansion in same command block.
Whenever Windows command interpreter encounters an opening round bracket (
being interpreted as begin of a command block, it parses everything to matching parenthesis )
and replaces all environment variable references done with %VariableName%
by current value of the environment variable.
In posted code this means the line
if \i %str1%==%%n echo "true"
is changed by Windows command interpreter to
if \i == %n echo "true"
before FOR is executed at all because of environment variable str1
is not defined above the FOR command block.
This can be easily seen by changing echo off
to echo on
or remove the line with echo off
or comment it out with command rem
and run the batch file from within a command prompt window. Then Windows command interpreter outputs each command block and each command line after preprocessing before execution.
Double clicking on a batch file to execute it is not good as the window is automatically closed on an exit of batch processing because of a syntax error like this one. The usage of pause
is no help as this command line is not reached at all on detecting a syntax error by cmd.exe
.
A solution would be:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq tokens=2 delims=:" %%I in ("%USERPROFILE%\Desktop\result\telnetresult.txt") do (
set "str1=Connect failed"
echo %%I
if /I "!str1!" == "%%~I" echo true
)
endlocal
echo/
pause
But much easier and also working would be:
@echo off
for /F "usebackq tokens=2 delims=:" %%I in ("%USERPROFILE%\Desktop\result\telnetresult.txt") do (
echo %%I
if /I "Connect failed" == "%%~I" echo true
)
echo/
pause
For the reason using echo/
instead of echo.
to output an empty line see What does an echo followed immediately by a slash do in a Windows CMD file?
The usage of I
or any other upper case letter instead of n
as loop variable is more safe. Why? Run in a command command window for /?
and read the output help explaining also %~nI
. On usage of %%~n
in a batch file it could be unclear for Windows command interpreter if the current value of loop variable n
should be used with surrounding double quotes removed or there is a syntax error as the loop variable is missing after modifier ~n
. Loop variables are case-sensitive. The usage of upper case letters avoids conflicts in interpretation of loop variables with modifiers.
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.
echo /?
endlocal /?
for /?
if /?
pause /?
set /?
setlocal /?
See Wikipedia article about Windows Environment Variables for a list of predefined environment variables with description like USERPROFILE.