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

发布于 2024-10-16 18:18:52 字数 572 浏览 2 评论 0原文

是否可以过滤 Windows 批处理文件中的参数列表?

假设它的调用方式如下:

file.cmd arg1 --unwanted arg3

它应该调用一些其他可执行文件(硬编码),而不带参数字符串--unwanted

some.exe arg1 arg3

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

更多示例(什么叫 -> 在结果中应该叫什么)

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 脚本编写 Windows 批处理文件对我来说是黑魔法:)

Is it possible to filter list of arguments in windows batch file?

Assuming it is called like:

file.cmd arg1 --unwanted arg3

It should call some other executable (hardcoded) without the argument string --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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

血之狂魔 2024-10-23 18:18:52
@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"

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

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文