在Windowsbatch file中过滤参数

是否有可能在Windowsbatch file中过滤参数列表?

假设它被称为:

file.cmd arg1 --unwanted arg3 

它应该调用一些其他的可执行文件(硬编码)没有参数string--unwanted

 some.exe arg1 arg3 

我只知道参数的名称,它可以作为列表中的任何parameter passing。 它可能不存在于参数列表中,并且所有参数都应该不加修改地传递。 参数数量可能会有所不同。

更多的例子(所谓的 – >应该叫什么结果)

 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 

从UNIX的shell脚本移动batch file是黑魔法给我:)

 @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% 

这打印:

 arg1 --unwanted arg3>>arg1 arg3 arg1 arg2>>arg1 arg2 --unwanted>> --unwanted arg2>>arg2 arg1 --unwanted>>arg1 

编辑:

 @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 for /F "tokens=%var% delims= " %%A in ("%*") do ( if not "%%~A"=="--unwanted" set "params=%params%%%A " goto nextparam ) echo.%*^>^>%params% 

版画

 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" 

意思是“arg1 = foo”被破坏了,你可以把for循环切换到for /F "tokens=%var% delims= " %%A in ('echo.%*') do ( arg1 = foo和arg3 =“bar”

这是批处理文件的一个已知问题,一旦你开始搞乱字符串,在某些配置中就会搞砸了,因为总会有一些字符会把事情搞乱(%,!,“,^,&,| ,<,>,空格等)