测试批处理文件中参数是否为空的正确方法是什么? [英] What is the proper way to test if a parameter is empty in a batch file?

查看:90
本文介绍了测试批处理文件中参数是否为空的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试是否设置了变量.我已经尝试了几种技术,但是只要%1被引号引起来,例如%1"c:\some path with spaces"的情况,它们似乎都会失败.

I need to test if a variable is set or not. I've tried several techniques but they seem to fail whenever %1 is surrounded by quotes such as the case when %1 is "c:\some path with spaces".

IF NOT %1 GOTO MyLabel // This is invalid syntax
IF "%1" == "" GOTO MyLabel // Works unless %1 has double quotes which fatally kills bat execution
IF %1 == GOTO MyLabel // Gives an unexpected GOTO error.

根据此站点,这些是受支持的IF语法类型.所以,我看不出有办法.

According to this site, these are the supported IF syntax types. So, I don't see a way to do it.

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

推荐答案

使用方括号代替引号:

IF [%1] == [] GOTO MyLabel

括号是不安全的:只能使用方括号.

Parentheses are insecure: only use square brackets.

这篇关于测试批处理文件中参数是否为空的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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