{"id":778,"date":"2008-02-15T13:23:32","date_gmt":"2008-02-15T21:23:32","guid":{"rendered":"http:\/\/www.scriptinganswers.com\/essentials\/index.php\/2008\/02\/15\/running-command-line-tools\/"},"modified":"2008-11-04T08:26:26","modified_gmt":"2008-11-04T16:26:26","slug":"running-command-line-tools","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/02\/15\/running-command-line-tools\/","title":{"rendered":"Running command-line tools"},"content":{"rendered":"<p>These examples show how to run command-line tools from within a VBScript, capturing the tool&#8217;s output.<\/p>\n<p><code>'<br \/>\n' ScriptingAnswers.com Essentials - by Don Jones<br \/>\n'<br \/>\n' Run cmdline tools<br \/>\n'\tThis shows how to launch a command-line<br \/>\n'\ttool and retrieve its output into a string variable<br \/>\n'\tto then do something with it. For example, you might<br \/>\n'\tuse the InStr() function to determine if a specific<br \/>\n'\tstring exists in the command's output.<\/code><\/p>\n<p>&#8216; The trick is to use the WshShell object&#8217;s Exec, not Run, method<br \/>\nDim objShell, objExec<br \/>\nSet objShell = CreateObject(&#8220;WScript.Shell&#8221;)<\/p>\n<p>&#8216;run the command-line command &#8211; just as you&#8217;d type it into<br \/>\n&#8216;Start &gt; Run. Avoid running Cmd.Exe directly, because it<br \/>\n&#8216;doesn&#8217;t actually return any output itself.<br \/>\nSet objExec = objShell.Exec(&#8220;dir&#8221;)<\/p>\n<p>&#8216;objExec now represents the executing command. Our script<br \/>\n&#8216;doesn&#8217;t exactly wait for that to finish, but we can<br \/>\n&#8216;keep in touch with it. objExec is an instance of the<br \/>\n&#8216;WshScriptExec object, if you want to read the docs on it<\/p>\n<p>&#8216;we can wait for it to finish running<br \/>\nDo Until objExec.Status = 1<br \/>\nWScript.Sleep 100<br \/>\nLoop<\/p>\n<p>&#8216;we can get the exit code (note that this method<br \/>\n&#8216;isn&#8217;t in the WSH Type Library so it doesn&#8217;t show up<br \/>\n&#8216;in a lot of object browsers or code hinting menus,<br \/>\n&#8216;but it should work on the latest version of WSH)<br \/>\nWScript.Echo &#8220;Exited with &#8221; &amp; objExec.ExitCode<\/p>\n<p>&#8216;here&#8217;s where we get the output into a string var<br \/>\nDim strOutput<br \/>\nIf Not objExec.StdOut.AtEndOfStream Then<br \/>\nstrOutput = strOutput &amp; objExec.StdOut.Read(1)<br \/>\nEnd If<br \/>\nWScript.Echo strOutput<\/p>\n","protected":false},"excerpt":{"rendered":"<p>These examples show how to run command-line tools from within a VBScript, capturing the tool&#8217;s output. &#8216; &#8216; ScriptingAnswers.com Essentials &#8211; by Don Jones &#8216; &#8216; Run cmdline tools &#8216; This shows how to launch a command-line &#8216; tool and retrieve its output into a string variable &#8216; to then do something with it. For [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[283,24],"tags":[342,346,55,56,354,379,386,388,394,996],"class_list":["post-778","post","type-post","status-publish","format-standard","hentry","category-howto","category-vbscript","tag-applications","tag-capture","tag-cli","tag-command-line","tag-external","tag-run","tag-stdout","tag-utilities","tag-vbs","tag-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=778"}],"version-history":[{"count":1,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/778\/revisions"}],"predecessor-version":[{"id":799,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/778\/revisions\/799"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}