Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to "nest" the FOR command  (Read 10127 times)

0 Members and 1 Guest are viewing this topic.

MentalDrow

  • Guest
How to "nest" the FOR command
« on: November 07, 2005, 12:27:07 PM »
Thanks to Sidewinder, I now have an automated process for getting the IP Addresses for 120+ machines using a file that has a list of all the computers I manage by name.  I even got the info to pipe into another file so I can use the IP Addresses for something else.

Here's the code for anyone else who may need it.  I modified it so it prints the machine name with the IP address next to it.  Thanks go out to Sidewinder for the initial coding, I just modified it to include the machine name and pipe it to a .txt file.

Code: [Select]
@ECHO OFF
IF EXIST IP_List.txt del IP_List.txt
For /f %%a in (Computer_List.txt) do (ping -n 1 %%a | for /f "tokens=1-4* delims=: " %%i in ('find "Reply"') do (echo Workstation is %%a IP Address is %%k >> IP_List.txt))


To quote Sidewinder when he provided it to me "Yeah yeah, it's *censored* ugly but so is batch code." or something very close to that.

What I'd like to figure out next is the process for "nesting" the FOR command.  I see it in the code above but don't really understand the syntax.  If anyone (including you, Sidewinder) can help me with this by replying or pointing me to an online resource that specifically deals with nesting FOR commands, I'd appreciate it.  I'm trying to combine the "ping" command with another command (nbtstat -a most likely) that will allow me to strip out the MAC Address of the machines as well.  I want to have all three pieces of info "pipe" into the text document

Example
Code: [Select]
blah blah blah ECHO The Machine Name is %%a the IP Address is %%k and the MAC Address is %%x >> IP_List.txt

Ordinarily I'd go direct with Sidewinder but I figure, just like the code at the top of this post, someone could probably use the same info I'm looking for so I'll ask the question here and everyone else can benefit from the replies I get.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: How to "nest" the FOR command
« Reply #1 on: November 07, 2005, 03:19:44 PM »
Code: [Select]

@ECHO OFF
IF EXIST IP_List.txt del IP_List.txt
For /f %%a in (Computer_List.txt) do (
     ping -n 1 %%a | for /f "tokens=1-4* delims=: " %%i in ('find "Reply"') do (
           nbtstat -a %%a | for /f "tokens=1-2 delims=^=" %%x in ('find "MAC Address"') do (
           echo Workstation is %%a IP Address is %%k Mac Address is %%y >> IP_List.txt
           )
     )
)


Good luck with finding documentation on nested FOR statements. Even the MS documentation for a single FOR is severely lacking. Personally, I find it easier to write them backwards...start with the innermost FOR and develop the outer FORs around it. On the other hand, if I were administering 120+ machines I would seriously consider using Windows Script but that's for another day.

Good luck.  8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

MentalDrow

  • Guest
Re: How to "nest" the FOR command
« Reply #2 on: November 07, 2005, 05:08:38 PM »
Quote
On the other hand, if I were administering 120+ machines I would seriously consider using Windows Script but that's for another day.

Good luck.  8)


Ordinarily, so would I but "The Powers that Be" limit how much us "Powers that Wanna Be" have access to.  Besides, I know even less about scripting that I do .bat but that'll change too if I have any say in the matter. ;D

EDIT: It looks like the | is used after each do (XXX) statement to bring the next FOR command online before it goes to the next item in computer_list.txt.  I had thought this was the case but wasn't sure how to tie it in.  Thanks for the help.  I can use this for other similar things where I want to pull a list of things and run a process on it.  Dude, you RULE! 8)
« Last Edit: November 07, 2005, 05:16:29 PM by MentalDrow »

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: How to "nest" the FOR command
« Reply #3 on: November 07, 2005, 06:32:23 PM »
Quote
It looks like the | is used after each do (XXX) statement to bring the next FOR command online before it goes to the next item in computer_list.txt.


Not quite. Nesting FORs is much like creating loops within loops, each with its own defined variable(s). Your first FOR sets up a loop to read the Computer_List file one record at a time. Each record (computername) is fed into a PING command (that's the purpose of the | aka: a pipe). The PING then pings a computer by name and returns an IP address, which the next FIND will parse according to the rules setup by the preceeding FOR. At this point you have a computername and an IP address. Next up, you take the computername and pass it to NBTSTAT, which returns a boatload of output and again, you go thru and search  for "MAC Address". When it's found, the preceeding FOR has setup the parsing rules, and you can extract the  the MAC address which is assigned another variable. Finally, you have variables (%%a, %%k, and %%y) with info you want, and output them to a file. The closing parens just close each FOR loop and it's back to the beginning to get another computername.

When passing data thru the pipe from one command to another, it's easier if you run each command from the command line, check it's output and then setup the parsing rules accordingly. Of course its also a good thing if you know what command produces what output. ;D

Well, I'm sufficently dizzy. I sure hope DOS is not back to the future. But wait, Microsoft's new MSH/Monad is gonna make batch coders wish they never heard of batch language (think: batch language scripting on steroids).

8)
« Last Edit: November 07, 2005, 07:20:12 PM by Sidewinder »
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Abo-Zead



    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: How to "nest" the FOR command
« Reply #4 on: December 28, 2020, 08:43:38 PM »
How we can add the serialNumber of these devices also