ECHO Arguments count = !ARGC!
FOR /L %%I IN (1,1,!ARGC!) DO (
CALL :GETARG %%I ARGI
ECHO Argument #%%I = [ !ARGI! ] )
CALL :GETARGS 1 !ARGC! ARGS
ECHO Arguments = [ !ARGS! ]
ENDLOCAL
EXIT /B 0
:GETARG
SET %2=!ARG%1!
SET %2_=!ARG%1_!
SET %2Q=!ARG%1Q!
EXIT /B 0
:GETARGS
SET %3=
FOR /L %%I IN (%1,1,%2) DO (
IF %%I == %1 (
SET %3=!ARG%%I!
) ELSE (
SET %3=!%3! !ARG%%I!
)
)
EXIT /B 0
:PARSE_ARGV
SET PARSE_ARGV_ARG=[]
SET PARSE_ARGV_END=FALSE
SET PARSE_ARGV_INSIDE_QUOTES=FALSE
SET /A ARGC = 0
SET /A PARSE_ARGV_INDEX=1
:PARSE_ARGV_LOOP
CALL :PARSE_ARGV_CHAR !PARSE_ARGV_INDEX! "%%ARGV:~!PARSE_ARGV_INDEX!,1%%"
IF ERRORLEVEL 1 (
EXIT /B 1
)
IF !PARSE_ARGV_END! == TRUE (
EXIT /B 0
)
SET /A PARSE_ARGV_INDEX=!PARSE_ARGV_INDEX! + 1
GOTO :PARSE_ARGV_LOOP
:PARSE_ARGV_CHAR
IF ^%~2 == ^" (
SET PARSE_ARGV_END=FALSE
SET PARSE_ARGV_ARG=.%PARSE_ARGV_ARG:~1,-1%%~2.
IF !PARSE_ARGV_INSIDE_QUOTES! == TRUE (
SET PARSE_ARGV_INSIDE_QUOTES=FALSE
) ELSE (
SET PARSE_ARGV_INSIDE_QUOTES=TRUE
)
EXIT /B 0
)
IF %2 == "" (
IF !PARSE_ARGV_INSIDE_QUOTES! == TRUE (
EXIT /B 1
)
SET PARSE_ARGV_END=TRUE
) ELSE IF NOT "%~2!PARSE_ARGV_INSIDE_QUOTES!" == " FALSE" (
SET PARSE_ARGV_ARG=[%PARSE_ARGV_ARG:~1,-1%%~2]
EXIT /B 0
)
IF NOT !PARSE_ARGV_INDEX! == 1 (
SET /A ARGC = !ARGC! + 1
SET ARG!ARGC!=%PARSE_ARGV_ARG:~1,-1%
IF ^%PARSE_ARGV_ARG:~1,1% == ^" (
SET ARG!ARGC!_=%PARSE_ARGV_ARG:~2,-2%
SET ARG!ARGC!Q=%PARSE_ARGV_ARG:~1,-1%
) ELSE (
SET ARG!ARGC!_=%PARSE_ARGV_ARG:~1,-1%
SET ARG!ARGC!Q="%PARSE_ARGV_ARG:~1,-1%"
)
SET PARSE_ARGV_ARG=[]
SET PARSE_ARGV_INSIDE_QUOTES=FALSE
)
EXIT /B 0
Very tricky in batch, but this code is fabulous!
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET ARGV=.%*
CALL :PARSE_ARGV
IF ERRORLEVEL 1 (
ECHO Cannot parse arguments ENDLOCAL EXIT /B 1
)
ECHO Arguments count = !ARGC!
FOR /L %%I IN (1,1,!ARGC!) DO (
CALL :GETARG %%I ARGI
ECHO Argument #%%I = [ !ARGI! ] )
CALL :GETARGS 1 !ARGC! ARGS
ECHO Arguments = [ !ARGS! ]
ENDLOCAL
EXIT /B 0
:GETARG
SET %2=!ARG%1!
SET %2_=!ARG%1_!
SET %2Q=!ARG%1Q!
EXIT /B 0
:GETARGS
SET %3=
FOR /L %%I IN (%1,1,%2) DO (
IF %%I == %1 (
SET %3=!ARG%%I!
) ELSE (
)
EXIT /B 0
:PARSE_ARGV
SET PARSE_ARGV_ARG=[]
SET PARSE_ARGV_END=FALSE
SET PARSE_ARGV_INSIDE_QUOTES=FALSE
SET /A ARGC = 0
SET /A PARSE_ARGV_INDEX=1
:PARSE_ARGV_LOOP
CALL :PARSE_ARGV_CHAR !PARSE_ARGV_INDEX! "%%ARGV:~!PARSE_ARGV_INDEX!,1%%"
IF ERRORLEVEL 1 (
EXIT /B 1
)
IF !PARSE_ARGV_END! == TRUE (
)
SET /A PARSE_ARGV_INDEX=!PARSE_ARGV_INDEX! + 1
GOTO :PARSE_ARGV_LOOP
:PARSE_ARGV_CHAR
Man Thanks to the author of this page: http://skypher.com/index.php/2010/08/17/batch-command-line-arguments/