{"id":85938,"date":"2004-08-30T12:37:13","date_gmt":"2004-08-30T20:37:13","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/scripting\/?p=85938"},"modified":"2019-06-03T12:39:11","modified_gmt":"2019-06-03T20:39:11","slug":"how-can-i-stop-a-script-partway-through","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-stop-a-script-partway-through\/","title":{"rendered":"How Can I Stop a Script Partway Through?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/p>\n<p>Hey, Scripting Guy! Is there a way to make a script stop itself if certain conditions aren\u2019t met? Like say I\u2019m going to copy files to a remote computer, but then the remote computer isn\u2019t available. Can I write code that tells the script to just go ahead and quit at that point?<\/p>\n<p>&#8212; WK, Palo Alto, CA<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\" \/><\/a><\/p>\n<p>Hey, WK. This is one of the easiest questions we\u2019ve ever had to answer: you can stop a script at any time by using a single line of code:<\/p>\n<pre class=\"codeSample\">Wscript.Quit\r\n<\/pre>\n<p>That\u2019s it; as soon as that line is executed, the script stops right then and there.<\/p>\n<p>So how would you actually make use of this command? Well, let\u2019s take your scenario, in which you try to connect to a remote computer and you fail; in that case, an error will be generated. You can then check the value of the Err object and if it\u2019s anything other than 0 (0 means no error occurred) you can exit the script right then and there:<\/p>\n<pre class=\"codeSample\">If Err &lt;&gt; 0 Then\r\n    Wscript.Quit\r\nEnd If\r\n<\/pre>\n<p>Or maybe before quitting you might want to echo a message explaining <i>why<\/i> the script quit:<\/p>\n<pre class=\"codeSample\">If Err &lt;&gt; 0 Then\r\n    Wscript.Echo \"Unable to contact remote computer; \" &amp; _\r\n        \"the script will now quit.\"\r\n    Wscript.Quit\r\nEnd If\r\n<\/pre>\n<p>For more information about the Quit method, see this portion of the <a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/script56\/html\/wsMthQuit.asp\" rel=\"noopener\">Windows Script Host documentation<\/a> on MSDN<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there a way to make a script stop itself if certain conditions aren\u2019t met? Like say I\u2019m going to copy files to a remote computer, but then the remote computer isn\u2019t available. Can I write code that tells the script to just go ahead and quit at that point? &#8212; WK, [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-85938","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there a way to make a script stop itself if certain conditions aren\u2019t met? Like say I\u2019m going to copy files to a remote computer, but then the remote computer isn\u2019t available. Can I write code that tells the script to just go ahead and quit at that point? &#8212; WK, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/85938","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=85938"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/85938\/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=85938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=85938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=85938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}