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

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

问题描述

我的操作系统是 Windows Vista.我需要有一个.bat"文件,我需要在其中检查用户是否输入了任何命令行参数.如果是,那么如果参数等于 -b 那么我会做一些事情,否则我会标记无效输入".如果用户没有输入任何命令行参数,那么我会做一些事情.我创建了以下 .bat 文件.它适用于 -b 和不等于 -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.

我总是出错:

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!

推荐答案

需要检查参数是否为空:if "%~1"=="" goto blank

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

一旦你这样做了,然后在 -b 上做一个 if/else 开关:if "%~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. "~" ensures double quotes are stripped if they were on the command line argument.

这篇关于如何检查“.bat"中的命令行参数文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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