Em geral, @DavidPostill tem a resposta correta. Ele não verá /?
switches (e possivelmente alguns outros). Se você gostaria de vê-los, então você pode usar: for %%x in ("%*") do (
em vez de for %%x in (%*) do (
. O problema é que esta versão não verá nada entre aspas. Se você quiser uma versão que possa fazer as duas coisas, aqui está uma resposta decididamente menos simples:
@set @junk=1 /*
@ECHO OFF
:: Do not changes the above two lines. They are required in order to make the
:: JScript below work.
:: In order to get the parameter count from WSH we will call this script
:: three times in three different ways. The first time, it'll run the code in this
:: section just as any normal BATCH script would. At the end of this section, it'll
:: call cscript.exe in order to run the JScript portion below.
:: The final call will be the same call as was originally requested but with the
:: difference of the first parameter being the word redux (if that is a possible
:: valid value for your script then you'll want to change it here and in the
:: JScript below as well).
IF "%1" == "redux" @GOTO :CLOSINGTIME
:: The next step passes this script to get the WSH command line executable for
:: further processing.
cscript //nologo //E:jscript %~f0 %*
:: Exit the initial call to this script.
GOTO:EOF */
// We are now in the second iteration of the call. Here we are using JScript
// instead of batch because WSH is much better at counting it's parameters.
var args=WScript.Arguments,
sh=WScript.CreateObject("WScript.Shell"),
cmd="%comspec% /k " + WScript.ScriptFullName + ' redux ' + args.length;
for(var i=0, j=args.length; i<j; i++)
cmd+=' "' + args(i) + '"';
// sh.Popup("The generated command line is:\n "+cmd);
var exec=sh.Exec(cmd);
while(!exec.StdOut.AtEndOfStream)
WScript.Echo(exec.StdOut.ReadLine());
// Leave the script now. Remember that the entire script needs to be parsable by
// WSH so be sure that anything after this line is in the comment below.
WScript.Quit(0);
/* This line is here to hide the rest of the file from WSH.
========================================================================
:CLOSINGTIME
:: Now we've called this script 3 times (once manually and now twice more just
:: to get back here knowing the correct argument count. We've added that value
:: to the command line so lets remove that cruft before we call this done.
:: Remove the static, redux, parameter
SHIFT
:: Save the argument count.
SET ARGC=%1
:: Remove ARGC parameter
SHIFT
:: Now you are ready to use your batch code. The variable %ARGC% contains the
:: argument count of the original call.
:: ******************************
:: ** START OF YOUR BATCH CODE **
:: ******************************
ECHO Fancy JScript count: %ARGC%
ECHO.
:: ****************************
:: ** END OF YOUR BATCH CODE **
:: ****************************
:: This is needed in order to let the JScript portion know that output has ended.
EXIT
:: ========================================================================
:: This line will hide everything in your second BATCH portion from WSH. */
Infelizmente, essa não é uma resposta perfeita, seja pelo modo como tivemos que executar o arquivo no WSH, StdErr e StdOut estão quebrados para o script final. Você pode corrigir o StdErr em determinadas situações usando 2&>1
no final da segunda chamada em lote: var exec=sh.Exec(cmd+" 2&>1");
StdIn precisará ser tratado como um caso especial para cada script.