简体   繁体   English

Windows批处理文件中的过滤参数

[英]Filtering arguments in windows batch file

Is it possible to filter list of arguments in windows batch file? 是否可以过滤Windows批处理文件中的参数列表?

Assuming it is called like: 假设它被称为:

file.cmd arg1 --unwanted arg3

It should call some other executable (hardcoded) without the argument string --unwanted : 它应该调用其他一些不带参数字符串--unwanted可执行文件(硬编码):

some.exe arg1 arg3

I only know the name of the argument, it can be passed as any argument in the list. 我只知道参数的名称,它可以作为列表中的任何参数传递。 It may not exist on argument list, and the all arguments should be passed unmodified. 它可能不存在于参数列表中,所有参数都应原样传递。 Number of arguments may vary. 参数数量可能会有所不同。

More examples (what's called -> what should be 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 from unix shell scripting windows batch files are black magic to me :) 从unix shell脚本编写Windows批处理文件迁移对我来说是不可思议的:)

@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
for /F "tokens=%var% delims= " %%A in ("%*") do (
    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" is broken, you can switch the for loop to for /F "tokens=%var% delims= " %%A in ('echo.%*') do ( to get it working, but that breaks arg1=foo and arg3="bar" 意思是“ arg1 = foo”已损坏,您可以将for循环切换为for /F "tokens=%var% delims= " %%A in ('echo.%*') do (使它工作,但这会中断arg1 = foo和arg3 =“ bar”

This is a known problem with batch files, once you start messing with strings things are going to get screwed up in some configuration since there is always some character that is going to mess things up (%,!,",^,&,|,<,>,space etc) 这是批处理文件的一个已知问题,一旦您开始弄乱字符串,事情就会在某种配置中搞砸了,因为总有一些字符会弄乱事情(%,!,“,”,^,&,| ,<,>,空格等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM