title: How to pass a command with spaces and quotes as a single parameter to CScript?
body: <p>I'm using CScript to run a VBScript file, and I need to pass a command line to the script where the parameter includes both spaces and quotes.  The entire command needs to be passed as one parameter to the script.  For example:</p>
  
  <pre><code>C:\&gt; CScript myscript.vbs //Nologo "cmd.exe /c "dir &amp;&amp; del /q *.txt""
  </code></pre>
  
  <p>Note the specific command line above is just an example, but it does touch on what I'm trying to do (i.e. pass a command line that involves running cmd.exe).  I'm trying to solve for the general case, however.</p>
  
  <p>The issue is that CScript appears to be getting confused trying to determine where quotes are opening and closing.  At least sometimes.</p>
  
  <p>The following script may help explain.  It just outputs the passed in arguments as it sees them:</p>
  
  <pre><code>Dim count
  For count = 0 To WScript.Arguments.Count-1
      WScript.Echo CStr(count) &amp; " = " &amp; WScript.Arguments.Item(count)
  Next
  </code></pre>
  
  <p>Often times, it works as expected:</p>
  
  <pre><code>C:\&gt; cscript myscript.vbs //Nologo "1 2 3"
  0 = 1 2 3
  </code></pre>
  
  <p>But when there are quotes inside the quoted parameter, things change:</p>
  
  <pre><code>C:\&gt; cscript myscript.vbs //Nologo "1 "2 3""
  0 = 1 2
  1 = 3
  </code></pre>
  
  <p>I understand there is an introduced ambiguity about whether each quote is opening or closing a parameter in the above example, so next I tried escaping the internal quotes so the command processor will treat them as literal strings:</p>
  
  <pre><code>C:\&gt; cscript myscript.vbs //Nologo "1 ^"2 3^""
  0 = 1 ^2
  1 = 3
  </code></pre>
  
  <p>Hmmm, looks like that only works when the shell does your globbing and such for you.  I doubt cscript understands <code>^</code> as an escape character like cmd.exe does.</p>
  
  <p>Using single quotes inside the double-quoted command (e.g. "cmd.exe /c 'dir &amp;&amp; cd \'") properly parses as a single parameter but won't work because for the case of invoking cmd.exe with the /c parameter, single quotes are not sufficient.  For example:</p>
  
  <pre><code>C:\&gt; cmd /c 'dir &amp;&amp; cd \'
  ''dir' is not recognized as an internal or external command,
  operable program or batch file.
  </code></pre>
  
  <p>I suppose I could just iterate over all the arguments in the script and join them back to one long string, but I'll lose the internal quotes and context that they provide doing that.  Or maybe I do use single quotes inside the the double-quoted param and map those back to double quotes inside the script.  But what if the intention <em>was</em> single quotes for some future case I'm not considering?  Since I'm attempting to solve for the general case, I can't make that determination.</p>
  
  <p>Any ideas?</p>