How to do a nested FOR loop?

February 16, 2010 at 20:55:10
Specs: Windows XP
Here's my goal:
1) grab name1 in LIST.TXT, which is a text file with 20 computer names.
2) Find the computer on the network, go to Logfile.txt and find 2010-02-16 in it.
3) Return the line that starts with value 2010-02-16 and everything after it.
4) write the computername first and then the 2010-02-16 line to results.txt in the same folder I'm running the batch file from
5) loop through and grab the next computername (name2) and repeat....

Here's my code that is no doubt way off..
@echo off
setlocal EnableDelayedExpansion
FOR F %%Z IN (LIST.TXT) DO (
for /f "tokens=1* delims=," %%a in ('find "2010-02-16"^<"C:\Program Files\PirateProgram\Logfile.txt"') do echo %%a>>C:\batchfiles\testing\results.txt
)

I've done FOR loops before, but never have tried to nest them. I start getting totally confused with the logic! Can one do this? Thanks!


See More: How to do a nested FOR loop?

Report •


#1
February 16, 2010 at 22:11:19
other than syntax, your approach should work ok.
this: FOR F %%Z IN (LIST.TXT) DO (
just needs to be: FOR /F %%Z IN (LIST.TXT) DO (
but i'm sure there's more unresolved issues...
"Find the computer on the network"
how define "find the computer" - various ways & means & results


Report •

#2
February 16, 2010 at 22:15:55
"2) Find the computer on the network, go to Logfile.txt
and find 2010-02-16 in it."

I'm a little confused, are you trying to trying to find a
text document on the network or the local drive
(C:\Program Files\PirateProgram\Logfile.txt)?

If your trying to access the file from the network
computers you will need the share name not the local name
of the file(i.e \\%%Z\nameofshare\Logfile.txt) or a share
mapped to a drive letter.

Other than that this would be my guess(provided you actually
want the local file):

@echo off
setlocal EnableDelayedExpansion
FOR /F %%Z IN (LIST.TXT) DO (
    for /f "skip=2 tokens=1* delims=," %%a in ('find "2010-02-16" "C:\Program Files\PirateProgram\Logfile.txt"') do (
        >>C:\batchfiles\testing\results.txt echo %%Z %%a
    )
)


Report •

#3
February 16, 2010 at 22:17:19
actually, i meant to put the /F there. As for finding the computer, that's working, as list.txt is just a line delimted text file.
What this is not doing is writing any ouput anywhere. It's not creating an output file.

Report •

Related Solutions

#4
February 16, 2010 at 22:24:45
It just occurred to me, are you trying to splice the output so that you end up with something like this:

computernames

blah
blahblahbalh
wooo

logfile

2010-02-16 sodsfsfsdf
some other junk
2010-02-16 fvfgfgf
dfsdfds
2010-02-16 ddddd

Output

blah 2010-02-16 sodsfsfsdf
blahblahbalh 2010-02-16 fvfgfgf
wooo 2010-02-16 ddddd

Is this what your after?


Report •

#5
February 16, 2010 at 22:33:21
the network computer name was what I was needing judago so thanks for that. after putting that in and adding the closed parentheses.. and the forward slash (/F) that nbrane showed me i was missing (I knew this.. just didn't catch that I did it), it worked perfectly!! I actually can't believe I came as close as I did... hehe.

Thanks a bunch guys.. I sure appreciate it!


Report •

#6
February 16, 2010 at 22:38:00
All I'm doing is scanning all the networked computers to make sure they have the install line in that logfile. So long as they have that, I know my batch file install (using psexec) worked.
My output looks like this:
COMPUTER1 2010-02-16 17:25 My Application Complete C:\Program Files\PirateProgram

With the output, I figure I can just browse the list to see any unsuccessful installs... instead of logging in to every machine.


Report •

#7
February 17, 2010 at 09:13:48
Let's take it 1 step further. Everything works.. but now how to include "ERROR" ouput in results.txt next to a computer if it fails. In other words, if my search doesn't find that line i'm searching for, i'd like to write the following entry in results.txt (where name1 is %%Z in my loop of course):
name1 ERROR

Report •

#8
February 17, 2010 at 13:31:15
judago.. why the "skip=2" in your script? Wouldn't that skip the first 2 computers in the list?

Report •

#9
February 17, 2010 at 17:16:39
Error ouput(more info).:

@echo off
FOR /F %%Z IN (LIST.TXT) DO (
    for /f "skip=2 tokens=1* delims=," %%a in ('
        find "2010-02-16" "C:\Program Files\PirateProgram\Logfile.txt"^|^|echo ERROR
    ') do (
        >>C:\batchfiles\testing\results.txt echo %%Z %%a
    )
)

"judago.. why the "skip=2" in your script? Wouldn't
that skip the first 2 computers in the list?"

Find has an annoying habit of outputting a header when
given a file name, it doesn't when passed data through a
pipe. Personally I use "skip=2" but passing the text through
"|" or "<" it a pipe works equally well to remove the header:

D:\Home\DATA>find "a" logfile.txt

---------- LOGFILE.TXT

D:\Home\DATA>find "a"<logfile.txt

D:\Home\DATA>


Report •

#10
February 18, 2010 at 12:52:02
Thanks for the explanation judago. Love your site too! I've been looking everywhere for good references to learn this stuff. I really enjoy it.. I've got ya' bookmarked!
Peace

Report •

#11
February 18, 2010 at 15:10:06
Thanks, I'm flattered ;)

Report •

#12
February 20, 2010 at 10:19:06
Worked great. Thanks!

Report •


Ask Question