Windows Shell 脚本:检查包含双引号的批处理选项

发布于 2024-08-13 01:21:02 字数 430 浏览 4 评论 0原文

尊敬的专家们,您好!

我想在我的批处理脚本中检查参数(或参数)字符串是否存在:

if "%*"=="" findstr "^::" "%~f0"&goto :eof

如果没有任何参数包含在双引号中,则效果很好。例如:

test.bat par1 par2 ... --- works

test.bat "par 1" par2 ... --- fails

我的问题是:

1)有什么方法可以克服这个问题,而不是要求使用非双引号符号来指定“长”参数,然后使用字符串替换?

2)我可以使用“if”来比较两个同时包含双引号和空格的字符串吗?

非常感谢您及时、明确的答复。

Greetings, dear Experts!

I want to check the existense of parameter (or argument) sting at all in my batch script:

if "%*"=="" findstr "^::" "%~f0"&goto :eof

This works fine if none of parameters is enclosed in double quotes. For example:

test.bat par1 par2 ... --- works

but

test.bat "par 1" par2 ... --- fails

My question are:

1) Is there any way to overcome this instead of requirement for use to use non-double-quoted symbol to specify "long" arguments and then use string substitution?

2) Can I ever use "if" to compare two strings containing both double quotes and spaces?

Your prompt and clear reply would be very much appreciated.

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

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

发布评论

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

评论(3

疯了 2024-08-20 01:21:02

~ 将去掉双引号,但对 %* 不起作用,但如果您只想知道是否没有参数,只需检查 %1 就足够了

if "%~1"==""

您可能需要先调用 setlocal ENABLEEXTENSIONS 以确保扩展已打开(需要~)

~ will strip the double quotes, but does not work on %*, but if you just want to know if there are no parameters, just checking %1 should be enough

if "%~1"==""

You might want to call setlocal ENABLEEXTENSIONS first to make sure extensions are on (required for ~)

愛放△進行李 2024-08-20 01:21:02

谢谢你,安德烈斯。
这里还有两段代码来检查传递参数的存在性和数量:

set ArgumentString=%*
if not defined ArgumentString findstr "^::" "%~f0"&goto :eof
if "%ArgumentString:"=%"=="" findstr "^::" "%~f0"&goto :eof

set NumberOfArguments=0
for /l %%N in (1,1,9) do (
  call set CurrentArgument=%%%%N
  if defined CurrentArgument set /a NumberOfArguments += 1
)
if %NumberOfArguments% NEQ %N% findstr "^::" "%~f0"&goto :eof 

这里变量 N 包含所需的参数数量。

希望这对某人有帮助!

Thank you, Andres.
Here are two more pieces of code to check the existence and number of passed parameters:

set ArgumentString=%*
if not defined ArgumentString findstr "^::" "%~f0"&goto :eof
if "%ArgumentString:"=%"=="" findstr "^::" "%~f0"&goto :eof

set NumberOfArguments=0
for /l %%N in (1,1,9) do (
  call set CurrentArgument=%%%%N
  if defined CurrentArgument set /a NumberOfArguments += 1
)
if %NumberOfArguments% NEQ %N% findstr "^::" "%~f0"&goto :eof 

Here variable N contains needed number of parameters.

Hope this helps for somebody!

听风吹 2024-08-20 01:21:02

由于 Andrey 的答案无法正确计算参数,因此即使有超过 9 个参数,这里也有一种可行的方法(它甚至保留了原始参数,因此 %1 仍然通过移动来指向第一个参数) 转移到子例程中):

@echo off
call :get_num_args %*
echo %NumArgs%
goto :eof

:get_num_args
  set NumArgs=0
  :loop
  if not [%1]==[] (set /a NumArgs+=1) else (goto :eof)
  shift
  goto loop
goto :eof

Since Andrey's answer fails to correctly count arguments, here is a way that does work, even with more than 9 arguments (and it even perserves the original ones, so %1 still points to the first one by moving the shifting into a subroutine):

@echo off
call :get_num_args %*
echo %NumArgs%
goto :eof

:get_num_args
  set NumArgs=0
  :loop
  if not [%1]==[] (set /a NumArgs+=1) else (goto :eof)
  shift
  goto loop
goto :eof
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文