批处理文件 - 命令行参数的数量 [英] Batch files - number of command line arguments

查看:26
本文介绍了批处理文件 - 命令行参数的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是将一些 shell 脚本转换为批处理文件,我似乎找不到一件事……那就是对命令行参数数量的简单计数.

Just converting some shell scripts into batch files and there is one thing I can't seem to find...and that is a simple count of the number of command line arguments.

例如.如果你有:

myapp foo bar

在壳中:

  • $# -> 2
  • $* -> foo bar
  • $0 -> myapp
  • $1 -> foo
  • $2 -> bar

批量处理

  • ??-> 2 <---- 什么命令?!
  • %* -> foo bar
  • %0 -> 我的应用
  • %1 -> foo
  • %2 -> 条形

所以我环顾四周,要么是看错了地方,要么是瞎了眼,但我似乎找不到一种方法来计算传入的命令行参数的数量.

So I've looked around, and either I'm looking in the wrong spot or I'm blind, but I can't seem to find a way to get a count of number of command line arguments passed in.

有没有类似于shell的$#"的批处理文件的命令?

Is there a command similar to shell's "$#" for batch files?

ps.我发现的最接近的是遍历 %1s 并使用shift",但我需要稍后在脚本中引用 %1、%2 等,这样不好.

ps. the closest i've found is to iterate through the %1s and use 'shift', but I need to refernece %1,%2 etc later in the script so that's no good.

推荐答案

谷歌搜索一下会得到以下结果来自 维基书籍:

Googling a bit gives you the following result from wikibooks:

set argC=0
for %%x in (%*) do Set /A argC+=1

echo %argC%

似乎 cmd.exe 已经从旧的 DOS 时代进化了一些 :)

Seems like cmd.exe has evolved a bit from the old DOS days :)

这篇关于批处理文件 - 命令行参数的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆