筛选windows批处理文件中的参数
是否可以筛选windows批处理文件中的参数列表 假设它被称为:筛选windows批处理文件中的参数,windows,windows-7,batch-file,Windows,Windows 7,Batch File,是否可以筛选windows批处理文件中的参数列表 假设它被称为: file.cmd arg1 --unwanted arg3 它应该调用其他一些不带参数字符串的可执行文件(硬编码)——不需要的: some.exe arg1 arg3 我只知道参数的名称,它可以作为列表中的任何参数传递。它可能不存在于参数列表中,并且所有参数都应在未修改的情况下传递。参数的数量可能会有所不同 更多示例(调用的内容->结果中应调用的内容) 从unix shell脚本化windows批处理文件移动对我来说是一种魔术
file.cmd arg1 --unwanted arg3
它应该调用其他一些不带参数字符串的可执行文件(硬编码)——不需要的
:
some.exe arg1 arg3
我只知道参数的名称,它可以作为列表中的任何参数传递。它可能不存在于参数列表中,并且所有参数都应在未修改的情况下传递。参数的数量可能会有所不同
更多示例(调用的内容->结果中应调用的内容)
从unix shell脚本化windows批处理文件移动对我来说是一种魔术:)
这张照片是:
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“令牌=%var%delims=“%%A in('echo.%*')do(
)使其工作,但这会中断arg1=foo和arg3=“bar”
这是批处理文件中的一个已知问题,一旦开始处理字符串,在某些配置中就会出现问题,因为总是有一些字符会把事情弄得一团糟(%,!,“,^,&,|,空格等)@Anders:这意味着您想让silk知道基本思想是将所有参数逐个存储到一个变量中,跳过不需要的参数,然后使用该变量将过滤后的参数列表传递给应用程序。:)@安德烈的理解不是问题,在没有批量知识的情况下写作是一个问题problem@Anders几乎就在这里,它从参数中剥离了“--pretty=格式:%H”:(@silk:剥离不是我的代码,是cmd.exethat@silk:Anders是对的。如果合适的话,将带“=”的参数放在双引号中可以解决这个问题。
@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"