Move parameter out of for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jarosss
Posts: 7
Joined: 02 Apr 2016 02:13

Move parameter out of for loop

#1 Post by jarosss » 02 Apr 2016 04:14

Hello,
I am trying to modify .bat script and have a problem with compare of two values.
Firstly I tried to do like this:

Code: Select all

for /f "usebackq tokens=1,2 delims=:" %%a in (`nslookup %2`) do (
    echo %%a:%%b ^</br^> >> %opt%
    echo %%b | FINDSTR /I /C:"%3"
if errorlevel 1 (
            echo ^<td class="tg-031f"^>NOT %3^</td^> >> %opt%
     ) else (
            echo ^<td class="tg-031s"^>OK %3^</td^> >> %opt%
    )
      )

%3 is read from file
So its looking if value %3 from the file is found inside string %%b (output is a string in 5 lines - the result is always in 4th line if it can help). If it is errorlevel 1 (values are not the same) the result is NOT, and if values are the same the result is OK.
It's working how I want but all this is saving to .html file and the tables doesn't create good because "if" is inside "for" loop and its looking %3 in each line separately (so in total there is 5 results).

The idea is to take "if" outside of for loop:

Code: Select all

set output=0 
 
for /f "usebackq tokens=1,2 delims=:" %%a in (`nslookup %2`) do (
    echo %%a:%%b ^</br^> >> %opt%
    echo %%b >> %output%
    )
 
echo %output% | FINDSTR /I /C:"%3"
if errorlevel 1 (
            echo ^<td class="tg-031f"^>NOT %3^</td^> >> %opt%
     ) else (
            echo ^<td class="tg-031s"^>OK %3^</td^> >> %opt%
     )


But here the problem is that errorlevel doesn't work. I would like to achive that script will try to find each %3 parameter for each run of for loop.

I hope it is clear what I wrote here :)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Move parameter out of for loop

#2 Post by aGerman » 02 Apr 2016 04:36

You can't redirect text to a variable. You have to use SET. But in case of a concatenation in a loop you would have to use EnableDelayedExpansion.
Wouldn't it be much easier to search in your %opt% file instead?

Code: Select all

FINDSTR /I /C:"%3" "%opt%"


Regards
aGerman

jarosss
Posts: 7
Joined: 02 Apr 2016 02:13

Re: Move parameter out of for loop

#3 Post by jarosss » 02 Apr 2016 08:56

I cannot search in %opt% because it will search in all past results. I want to find string %3 corresponding to %%b in last run of the loop (or in last result in %opt%).

Could you please explain how can I do it with EnableDelayedExpansion?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Move parameter out of for loop

#4 Post by foxidrive » 02 Apr 2016 09:30

This may work for you:

A comment here is the "%%b |" will include the space at the end of %%b string.

Code: Select all

    set "found="
    for /f "usebackq tokens=1,2 delims=:" %%a in (`nslookup %2`) do (
    echo %%a:%%b ^</br^> >> %opt%
    echo %%b | FINDSTR /I /C:"%3" >nul && set found=1
   )
if not defined found (
            echo ^<td class="tg-031f"^>NOT %3^</td^> >> %opt%
     ) else (
            echo ^<td class="tg-031s"^>OK %3^</td^> >> %opt%
    )

jarosss
Posts: 7
Joined: 02 Apr 2016 02:13

Re: Move parameter out of for loop

#5 Post by jarosss » 02 Apr 2016 10:10

Unfortunatelly the result is always OK
Only if the first run of the loop doesn't find %%b==%3 the answer in NOT, so I think it compares all %%b results from the begining every time and not only last one.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Move parameter out of for loop

#6 Post by Aacini » 02 Apr 2016 11:01

I am afraid your question is somewhat confusing; this is what I understood:

jarosss could wrote:Check the output from 'NSLOOKUP' and look if value %3 is found inside second token; report the result as NOT or OK, but just on line #4.

This code do that:

Code: Select all

setlocal EnableDelayedExpansion

for /f "usebackq tokens=1-3 delims=:" %%a in (`nslookup %2 ^| findstr /N "^"`) do (
   echo %%b:%%c ^</br^> >> %opt%
   if %%a equ 4 (
      set "token2=%%c"
      if "!token2:%3=!" equ "%%c" (
         echo ^<td class="tg-031f"^>NOT %3^</td^> >> %opt%
      ) else (
         echo ^<td class="tg-031s"^>OK %3^</td^> >> %opt%
      )
   )
)

Antonio

jarosss
Posts: 7
Joined: 02 Apr 2016 02:13

Re: Move parameter out of for loop

#7 Post by jarosss » 02 Apr 2016 11:53

Hi Antonio,
The below script doesn't work good

Aacini wrote:jarosss could wrote:
Check the output from 'NSLOOKUP' and look if value %3 is found inside second token; report the result as NOT or OK, but just on line #4.

The result of nslookup are 5 lines
The intersting value (the answer) is always in line 4

I modify my previous first loop to:

Code: Select all

for /f "usebackq tokens=1-2 delims=:" %%a in (`nslookup %2 ^| findstr "%3"`) do (
    echo %%a:%%b ^</br^> >> %opt%
::    echo %%b | FINDSTR /I /C:"%3"
if errorlevel 1 (
            echo ^<td class="tg-031f"^>NOT %3^</td^> >> %opt%
     ) else (
            echo ^<td class="tg-031s"^>OK %3^</td^> >> %opt%
    )
      )


The OK results are correct but NOT are not displaying because findstr is filtering even the %%b result

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Move parameter out of for loop

#8 Post by Aacini » 02 Apr 2016 12:49

As I said before, your question is confusing, and the way to made it clear is explaining the things, not showing examples of code that does not work...

I suggest you that, in first place, read this topic; after that, post an example of the format (NOT the real data you got) of what you want as result. For example, do you want something like this?

Code: Select all

%%a:%%b from line 1
%%a:%%b from line 2
%%a:%%b from line 3
%%a:%%b from line 4
NOT or OK of line 4
%%a:%%b from line 5

... or something like this?

Code: Select all

%%a:%%b from line 1
NOT or OK of line 1
%%a:%%b from line 2
NOT or OK of line 2
%%a:%%b from line 3
NOT or OK of line 3
%%a:%%b from line 4
NOT or OK of line 4
%%a:%%b from line 5
NOT or OK of line 5

Any other? I don't understand what you mean with "The intersting value (the answer) is always in line 4".

Antonio

jarosss
Posts: 7
Joined: 02 Apr 2016 02:13

Re: Move parameter out of for loop

#9 Post by jarosss » 02 Apr 2016 14:57

Code: Select all

for /f "usebackq tokens=1-2 delims=:" %%a in (`nslookup %2`) do (
    echo %%a:%%b ^</br^> >> %opt%
      )

The output of above (for each run) looks like:
Server: hostname.com
Address: 190.190.190.190
Name: name.com
Address: 200.200.200.200
Aliases: google.com


From the .txt file the lines like below are being read (and the loop runs as many times as many lines are added into file):
NSLOOKUP;google.com;200.200.200.200
NSLOOKUP;youtube.com;300.300.300.300


The next thing I would like to have is showing if %3 from .txt (in this case 200.200.200.200 or 300.300.300.300) was found as the output of each run of the loop (and it cannot look in previous runs of the loop).
The problem is that when I add "if" inside the loop, %3 is being looked separately in each line of the output and not in the whole output.


//edit
I did two loops and the result is correct now.
Is it possible to simplify this to one loop?

Code: Select all

for /f "usebackq tokens=1-2 delims=:" %%a in (`nslookup %2`) do (
    echo %%a:%%b ^</br^> >> %opt%
      )
echo ^</td^> >> %opt%

for /f "usebackq tokens=1-2 skip=4 delims=:" %%a in (`nslookup %2 ^| findstr /vb Aliases`) do (
    echo %%a:%%b 
    echo %%b | FINDSTR /I /C:"%3"
if errorlevel 1 (
            echo ^<td class="tg-031f"^>NOT %3^</td^> >> %opt%
     ) else (
            echo ^<td class="tg-031s"^>OK %3^</td^> >> %opt%
    )
      )

Post Reply