Filtering arguments in windows batch file -
is possible filter list of arguments in windows batch file?
assuming called like:
file.cmd arg1 --unwanted arg3
it should call other executable (hardcoded) without argument string --unwanted
:
some.exe arg1 arg3
i know name of argument, can passed argument in list. may not exist on argument list, , arguments should passed unmodified. number of arguments may vary.
more examples (what's called -> should called in result)
file.cmd arg1 arg2 -> some.exe arg1 arg2 file.cmd --unwanted -> some.exe file.cmd --unwanted arg2 -> some.exe arg2 file.cmd arg1 --unwanted -> some.exe arg1
moving unix shell scripting windows batch files black magic me :)
@echo off setlocal enableextensions if "%1"=="sotest" ( rem tests... call file.cmd arg1 --unwanted arg3 call file.cmd arg1 arg2 call file.cmd --unwanted call file.cmd --unwanted arg2 call file.cmd arg1 --unwanted goto :eof ) set params= :nextparam if "%~1"=="--unwanted" (shift&goto nextparam) if not "%~1"=="" (set "params=%params%%1 "&shift&goto nextparam) echo.%*^>^>%params%
this prints:
arg1 --unwanted arg3>>arg1 arg3 arg1 arg2>>arg1 arg2 --unwanted>> --unwanted arg2>>arg2 arg1 --unwanted>>arg1
edit:
@echo off setlocal enableextensions if "%~1"=="sotest" ( rem tests... call file.cmd arg1=foo --unwanted arg3=bar call file.cmd arg1 arg2 call file.cmd --unwanted call file.cmd --unwanted arg2 call file.cmd arg1 --unwanted call file.cmd "arg1" --unwanted "arg3" call file.cmd "arg1=foo" --unwanted arg3="bar" goto :eof ) set var=0 set params= :nextparam set /a var=%var% + 1 /f "tokens=%var% delims= " %%a in ("%*") ( if not "%%~a"=="--unwanted" set "params=%params%%%a " goto nextparam ) echo.%*^>^>%params%
prints
arg1=foo --unwanted arg3=bar>>arg1=foo arg3=bar arg1 arg2>>arg1 arg2 --unwanted>> --unwanted arg2>>arg2 arg1 --unwanted>>arg1 "arg1" --unwanted "arg3">>"arg1" "arg3" "arg1=foo" --unwanted arg3="bar">>"arg1 foo" arg3="bar"
meaning "arg1=foo" broken, can switch loop for /f "tokens=%var% delims= " %%a in ('echo.%*') (
working, breaks arg1=foo , arg3="bar"
this known problem batch files, once start messing strings things going screwed in configuration since there character going mess things (%,!,",^,&,|,<,>,space etc)
Comments
Post a Comment