如何检查“.bat”中的命令行参数文件? [英] How to check command line parameter in ".bat" file?

查看:570
本文介绍了如何检查“.bat”中的命令行参数文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的操作系统是Windows Vista。我需要有一个.bat文件,我需要检查用户是否输入任何命令行参数或不。如果这样,如果参数等于 -b 那么我会做一些事情,否则我会标记无效输入。如果用户没有输入任何命令行参数,那么我会做一些事情。我创建了以下.bat文件。它适用于 -b 和不等于 -b 案例 - 但是当用户没有传递任何命令-row参数。

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.

我总是遇到错误:

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!


推荐答案

您需要检查参数是否为空白: code>如果%1==goto blank

You need to check for the parameter being blank: if "%1"=="" goto blank

else switch on -b:如果%1== - b(goto specific)else goto unknown

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屋!

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