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

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

问题描述

我需要测试一个变量是否设置。我尝试了几种技巧,但是它们似乎在每当%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.

根据 this site ,这些是支持的 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

注意:原始答案是:

IF(%1)==()GOTO MyLabel

@jeb指出这是不安全的,@Synetech建议一个替代方案,所以我修改了答案。

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

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