How about this:
@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogCmd=%*
echo on
call %0 %LogCmd:/log=% >"%~dpn0.log" 2>&1
set LogCmd=
goto :eof
:LogIt
REM the start of your batch file
So, "/log" can be anywhere in the parameter, and it is not in %* ... of
course, it has that one variable. I guess it's either no new variables but
%* is changed, or one new variable and %* is unchanged. If %*:/log=%
worked, this would be perfect. I happen to have a parameter parser that is
not order specific, so I like allowing /log to be anywhere.
Ahh... one of the fun parts of tweaking with batch scripting to get the best
solution with least tradeoffs.

This is also one of the reasons I like
this newsgroup now that I've found it.
--
//David
IIS
This posting is provided "AS IS" with no warranties, and confers no rights.
//
David Wang said:
I've found this tip very useful, and here's my addition to what Matthias
posted.
This will preserve the %0 as well as not have a 9 parameter limit, at the
cost that there are two env vars to clear out now -- so the only limitation
is that /log has to be first -- but honestly, that limitation only exists
because this adds logging by prepending a code snippet. It should not be
hard for a batch file who parses parameters to change that first IF to parse
parameters like the batchfile... and then you do have a completely
integrated solution.
@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogFil=%~dpn0.log
set LogCmd=%0
shift
:LogLoop
if /I ?%1? NEQ ?? set LogCmd=%LogCmd% %1&shift&goto :LogLoop
call %LogCmd% >%LogFil% 2>&1
set LogFil=
set LogCmd=
goto :eof
:LogIt
REM the start of your batch file
Hello David,
thanks for the acknowledgement and the extension. You inspired me to
also enhance my version. No vars, no external call.
@echo off
if /I [%1] NEQ [/log] goto :GoOn
call :LogIt %* >"%~dpn0.log" 2>&1
goto :eof
:LogIt
shift
echo on
:GoOn
::your normal batch code
Working with %* inside the batch would still include the /log option.
This drawback doesn't exist in a change of your version. Handling of
escape chars in arguments is OTOH problematic when setting to vars.
@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogCmd=%*
echo on
call %0 %LogCmd:~5% >"%~dpn0.log" 2>&1
set LogCmd=
goto :eof
:LogIt
REM the start of your batch file