{"id":69883,"date":"2005-05-04T14:47:00","date_gmt":"2005-05-04T14:47:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/05\/04\/how-can-i-exit-a-for-each-loop\/"},"modified":"2005-05-04T14:47:00","modified_gmt":"2005-05-04T14:47:00","slug":"how-can-i-exit-a-for-each-loop","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-exit-a-for-each-loop\/","title":{"rendered":"How Can I Exit a For Each Loop?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! Is there any way to exit a loop in a script?<BR><BR>&#8212; MW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, MW. You know, when we started writing this column we intended it to be a simple little thing that answered those basic questions so many beginning scripters have:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>How can I determine the current date in a script? (Use VBScript\u2019s <B>Date<\/B> function.)<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>How can I echo messages to the command window instead of in a message box? (Run the script under the <B>CScript <\/B>scripting host.)<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>How can I tell whether a variable holds a numeric value? (Use the <B>IsNumeric<\/B> function.)<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Ah, but you know how it goes: soon more and more people were telling us how much they liked the column, and soon we began answering questions that allowed us to show off our scripting skills. Eventually we got so full of ourselves that we no longer answered questions like, \u201cHow can I show a dialog box that people can use to enter information?\u201d (use VBScript\u2019s <B>InputBox<\/B> function) but instead we only answered complicated and esoteric questions, questions like, \u201cHow can I write a script that will exorcise the ghost of Greta Garbo?\u201d Interesting and useful, but often-times a bit beyond what the beginning scripter was ready to tackle.<\/P>\n<TABLE class=\"dataTable\" id=\"EQD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Scoff if you will, but to date none of the Scripting Guys has <I>ever<\/I> been haunted by the ghost of Greta Garbo.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you probably all know, after a wild, non-stop night of partying with supermodels you can\u2019t help but wake up the next morning feeling a little remorseful. And the same thing is true here. We know that we\u2019ve been neglecting a good portion of our audience &#8211; the beginning scripter -and we\u2019ve vowed to never let that happen again. Because of that, every now and then we\u2019ll be sure the answer one of the more basic scripting questions; promise. And just to prove it, today we\u2019ll tell you how you can exit a For Each loop. (Obviously something people are interested in, because we\u2019ve gotten several questions on this very topic.)<\/P>\n<P>Did you just hear a noise? Sounded like a ghost or something\u2026.<\/P>\n<P>OK: exiting a loop. Let\u2019s say you have an NT 4 domain and you want to determine whether a particular user account exists in that domain. If you were running Active Directory this would be a no-brainer: you could just search for that account and have an answer is seconds. But this is NT 4 and no ADO provider exists for NT 4; that means you can\u2019t search the accounts database on an NT 4 domain.<\/P>\n<P>So how can you tell whether or not a user account exists in an NT 4 domain? Well, one way to do this is the good old-fashioned, brute force way: you retrieve all the accounts and check each one. And the easiest way to do that is in a For Each loop:<\/P><PRE class=\"codeSample\">Set objComputer = GetObject(&#8220;WinNT:\/\/fabrikam,domain&#8221;)<\/p>\n<p>For Each objItem in objComputer\n    If objItem.Name = &#8220;kenmyer&#8221; Then\n        Wscript.Echo &#8220;Account found.&#8221;\n    End If\nNext\n<\/PRE>\n<P>There\u2019s nothing wrong with this approach, and &#8211; depending on your needs &#8211; you might have no choice but to methodically slog your way through the entire collection of accounts. The only problem is that this can be very slow; on a test domain with about 30,000 accounts it took well over 2 minutes for our script to complete. If the account you\u2019re looking for happens to be the very last account in the collection there\u2019s not much you can do about that. But what if the account you\u2019re looking for happens to be the very first item in the collection? In that case you\u2019re going to immediately find what you\u2019re looking for but then have to wade through 29,999 more accounts just so you can complete the For Each loop.<\/P>\n<P>Well, unless you explicitly exit the loop that is:<\/P><PRE class=\"codeSample\">Set objComputer = GetObject(&#8220;WinNT:\/\/fabrikam,domain&#8221;)<\/p>\n<p>For Each objItem in objComputer\n    If objItem.Name = &#8220;kenmyer&#8221; Then\n        Wscript.Echo &#8220;Account found.&#8221;\n        Exit For\n    End If\nNext\n<\/PRE>\n<P>In our first script we checked each account to see if it had the name <I>kenmyer<\/I>; it if did we echoed back the message \u201cAccount found.\u201d We do the same thing here, but with one difference: after echoing the message we then call the <B>Exit For<\/B> statement. Exit For immediately takes us out of our loop; if kenmyer happens to be the first account found we\u2019ll never look at any of the other 29,999 accounts. That\u2019s it: call Exit For and you are out of the loop. (We Scripting Guys seem to <I>always<\/I> be out of the loop, but that\u2019s a different story.)<\/P>\n<P>If you\u2019d like to see the effects of Exit For, run this script, which simply writes the numbers 1 through 1,000 to the screen:<\/P><PRE class=\"codeSample\">For i = 1 to 1000\n    Wscript.Echo i\nNext\n<\/PRE>\n<P>Now try this script, which calls the Exit For statement if the variable i equals 3:<\/P><PRE class=\"codeSample\">For i = 1 to 1000\n    Wscript.Echo i\n    If i = 3 Then\n        Exit For\n    End If\nNext\n<\/PRE>\n<P>See the difference? If you can write code like that you\u2019ll never have to worry about the ghost of Greta Garbo ever again. <\/P>\n<P>You sure you didn\u2019t hear that? Sounds like chains rattling\u2026.<\/P>\n<P>Exit For works in either a For Each or a For Next loop. But what if you need to exit a Do loop? No problem: just call <B>Exit Do<\/B>. What if you want to exit a subroutine? Just call <B>Exit Sub<\/B>. To exit a function call <B>Exit Function<\/B>. In other words, there\u2019s no reason to ever again find yourself trapped in a never-ending loop; if you want out, just Exit. <\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there any way to exit a loop in a script?&#8212; MW Hey, MW. You know, when we started writing this column we intended it to be a simple little thing that answered those basic questions so many beginning scripters have: \u2022 How can I determine the current date in a script? [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,5],"class_list":["post-69883","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there any way to exit a loop in a script?&#8212; MW Hey, MW. You know, when we started writing this column we intended it to be a simple little thing that answered those basic questions so many beginning scripters have: \u2022 How can I determine the current date in a script? [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=69883"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69883\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=69883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}