<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Savory Ape &#187; Command-line interface</title>
	<atom:link href="http://savoryape.com/tag/command-line-interface/feed/" rel="self" type="application/rss+xml" />
	<link>http://savoryape.com</link>
	<description>Progressive Internet Solutions</description>
	<lastBuildDate>Tue, 09 Mar 2010 13:44:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Force Command-Line for VBScripts</title>
		<link>http://savoryape.com/2009/06/force-cmdline-vbs/</link>
		<comments>http://savoryape.com/2009/06/force-cmdline-vbs/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 17:05:55 +0000</pubDate>
		<dc:creator>chrishota</dc:creator>
				<category><![CDATA[MidSOUTH]]></category>
		<category><![CDATA[Command-line interface]]></category>
		<category><![CDATA[Microsoft Windows]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://savoryape.com/?p=34</guid>
		<description><![CDATA[Say you have a script—like the previous CIM Repository Listing script—that outputs hundreds of rows of text via WScript.Echo. On the command line, this is fine; the text will go scrolling by, and can be piped to the more command or grep (yes, really), or logged to a file. But if you make the mistake [...]]]></description>
			<content:encoded><![CDATA[<p>Say you have a script—like the previous <a href="http://savoryape.com/2009/06/list-win32-cimv2/">CIM Repository Listing</a> script—that outputs hundreds of rows of text via WScript.Echo.  On the command line, this is fine; the text will go scrolling by, and can be piped to the more command or grep (yes, really), or logged to a file.  But if you make the mistake of executing a script like this by double-clicking, you&#8217;ll get modal popup messages that prevent the rest of the script from running until you hit &#8220;OK&#8221; for each line.</p>
<p>You could change the default scripting environment to always be the command line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">CScript <span style="color: #000000; font-weight: bold;">//</span>H:CScript <span style="color: #000000; font-weight: bold;">//</span>NoLogo <span style="color: #000000; font-weight: bold;">//</span>S</pre></div></div>

<p>But then you have to treat the GUI-based scripts differently, and launch them from the command line with wscript.  Oh, bother.</p>
<p>It&#8217;s always been possible to just go ahead and launch vbscripts from the command line with the cscript command, but having to open a command prompt and type out the path to the script every time is a pain.  Why not have the script auto-detect how it was launched, and if it wasn&#8217;t launched from the command line, relaunch itself correctly?  Well, <a href="http://tek-tips.com/viewthread.cfm?qid=1175727">Tek-Tips user tsuji</a> has figured out how to do it.</p>
<p>By including this subroutine in your VBScript and putting a call to it at the top of the script, it will do just that.</p>
<p>The edits to this script from tsuji&#8217;s version are that instead of keeping the command window open after execution with the &#8220;/k&#8221; switch, I continue to use the &#8220;/c&#8221; (close command after completion) but chain the &#8220;pause&#8221; command at the end of the command.  This is a little-known (in the DOS/Windows world, anyway) way of doing multiple things at once:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">command1 <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> command2</pre></div></div>

<p>On *NIX, this will execute command2 after command1 completes, if it completes successfully (return code 0).  On Windows, however, command2 will always be run, no matter the outcome of command1.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Sub</span> force_cmdline
	<span style="color: #000080;">Dim</span> args : args=<span style="color: #800000;">&quot;&quot;</span>
	<span style="color: #000080;">Dim</span> i, wshshell
	<span style="color: #000080;">If</span> right(lCase(wscript.fullname),11)= <span style="color: #800000;">&quot;wscript.exe&quot;</span> <span style="color: #000080;">then</span>
		<span style="color: #000080;">For</span> i=0 <span style="color: #000080;">to</span> wscript.arguments.count-1
			args = args &amp; wscript.arguments(i) &amp; <span style="color: #800000;">&quot; &quot;</span>
		<span style="color: #000080;">Next</span>
		<span style="color: #000080;">Set</span> wshshell = createobject(<span style="color: #800000;">&quot;wscript.shell&quot;</span>)
		wshshell.Run wshshell.ExpandEnvironmentStrings(<span style="color: #800000;">&quot;%comspec%&quot;</span>) &amp; <span style="color: #800000;">&quot; /c cscript.exe //nologo &quot;</span><span style="color: #800000;">&quot;&quot;</span> &amp; wscript.scriptfullname &amp; <span style="color: #800000;">&quot;&quot;</span><span style="color: #800000;">&quot;&quot;</span> &amp; args &amp; <span style="color: #800000;">&quot; &amp;&amp; pause&quot;</span>
		<span style="color: #000080;">set</span> wshshell = <span style="color: #000080;">Nothing</span>
		WScript.Quit
	<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://savoryape.com/2009/06/force-cmdline-vbs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
