批处理如何得到参数个数?

seanerwu 2002-05-08 10:27:51
批处理如何得到参数个数?
...全文
4205 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
willsort 2002-09-21
  • 打赏
  • 举报
回复 2
To All:

其实各位的回答都不切合作者的本意,他想获得参数的个数,而不是想知道参数怎么去用,UNIX系列中有一个环境变量$#,可以直接获得参数个数,而DOS没有此项,这可以说是它的弱点。不过仍然可以通过以下的方法来间接达到目的。

获得参数个数无非是想对命令参数做出相应的分析,可以用SHIFT来完成,以下是一个示意的例子。如果在REM语句处有结构化的处理思想,可以避开获得参数个数这个难题。

:LOOP
IF [%1]==[] GOTO END
REM 保存参数,以做回归分析
SET PARAMETER=%PARAMETER% %1
REM 此处插入其他参数分析代码
SHIFT
:GOTO LOOP

:END

真到获得参数个数是很困难的,关键是需要一个计数的方法,而DOS命令集中没有这个功能。除非是利用文件的字节数计数(利用COPY,FIND,DIR)或者自编一个对环境变量进行各种运算的程序。而前一种方法速度慢,后一种方法通用性很差。

笑面佛_正版 2002-05-11
  • 打赏
  • 举报
回复
%0 就代表批处理的文件名,%1就是第一个参数,一直到%9,可有10个参数
rosson 2002-05-11
  • 打赏
  • 举报
回复
@echo off
:loop
choice/c:abce
if errorlevel 4 goto end
if errorlevel 3 goto c
if errorlevel 2 goto b
if errorlevel 1 goto a
goto loop
:a
@echo aaaaa
goto loop
:b
@echo bbbbb
goto loop
:c
@echo ccccc
goto loop
:end

WINDOWS\COMMAND里面有一个choice拷到批处理目录下
licome 2002-05-10
  • 打赏
  • 举报
回复
调用c程序吧!
lbaby 2002-05-10
  • 打赏
  • 举报
回复
Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

NOT Specifies that Windows should carry out the command only
if the condition is false.
ERRORLEVEL number Specifies a true condition if the last program run returned
an exit code equal to or greater than the number specified.
command Specifies the command to carry out if the condition is
met.
string1==string2 Specifies a true condition if the specified text strings
match.
EXIST filename Specifies a true condition if the specified filename
exists.
@echo off
:loop
choice/c:abce
if errorlevel 4 goto end
if errorlevel 3 goto c
if errorlevel 2 goto b
if errorlevel 1 goto a
goto loop
:a
@echo aaaaa
goto loop
:b
@echo bbbbb
goto loop
:c
@echo ccccc
goto loop
:end

EG:dO.bat
liubingqian 2002-05-09
  • 打赏
  • 举报
回复
好像没有这个功能,只能获取参数的内容。还是看看楼下有什么高见吧!
farspeed 2002-05-08
  • 打赏
  • 举报
回复
你执行批处理文件,然后再命令的后面 跟上你的参数就好了!

记得顺序问题,当然可以做成互动的类型

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧