如何检查命令行参数" .BAT"文件? [英] How to check command line parameter in ".bat" file?
问题描述
我的操作系统是Windows Vista中。我需要有一个蝙蝠的文件,我需要检查,如果用户输入任何命令行参数与否。如果确实则如果参数等于-b,那么我会做一些否则我会标记无效的输入。如果用户不输入任何命令行参数的话,我会做一些事情。我创建了以下.bat文件。它适用于-B和不等于-b案件 - 但是当用户没有通过任何命令行参数失败。
我总是得到错误:
GOTO是在这个时候意外。
块引用>谁能告诉我,我究竟做错了什么?
ECHO OFF
CLS
回声。IF [%1] == [/?] GOTO BLANKIF%1 == - BGOTO具体IF NOT%1 == - BGOTO UNKNOWN:具体具体ECHOGOTO DONE:空白ECHO无参数GOTO DONE:未知ECHO未知选项GOTO DONE:DONEECHO完成!
解决方案您需要检查的参数是空白的:
如果%1==转到空白
一旦你这样做,然后做一个如果-b /其他开关:
如果%1== - B(具体转到)转到其他未知
带有引号的参数,使检查之类的空白/空/缺少的参数更容易。
My OS is Windows Vista. I need to have a ".bat" file where I need to check if user enters any command-line parameter or not. If does then if the parameter equals to "-b" then I will do something otherwise I will flag "Invalid input". If user does not enter any command-line parameter then I will do something. I have created following .bat file. It works for "-b" and "not-equal to -b" cases - but it fails when user does not pass any command-line parameter. I always get error:
GOTO was unexpected at this time.
Can anyone tell me what am I doing wrong here?
ECHO OFF CLS ECHO. IF [%1]==[/?] GOTO BLANK IF %1=="-b" GOTO SPECIFIC IF NOT %1=="-b" GOTO UNKNOWN :SPECIFIC ECHO SPECIFIC GOTO DONE :BLANK ECHO No Parameter GOTO DONE :UNKNOWN ECHO Unknown Option GOTO DONE :DONE ECHO Done!
解决方案You need to check for the parameter being blank:
if "%1"=="" goto blank
Once you've done that, then do an if/else switch on -b:
if "%1"=="-b" (goto specific) else goto unknown
Surrounding the parameters with quotes makes checking for things like blank/empty/missing parameters easier.
这篇关于如何检查命令行参数" .BAT"文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!