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

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

问题描述

我需要一个变量被设置或不进行测试。我试过几种技术,但他们忽视了,每当%1 用双引号,如包围的情况下,当%1 C:\\用空格一些路径。

  IF NOT%1 GOTO MyLabel //这是无效的语法
IF%1==GOTO MyLabel //除非作品1%有双引号这致命杀死蝙蝠执行
IF%1 == GOTO MyLabel //给出了一个意想不到的GOTO错误。

根据本网站,这些都是支持的 IF 语法类型。所以,我没有看到一个办法做到这一点。

  IF [NOT] ERRORLEVEL number命令
IF [NOT]字符串1 ==字符串2命令
IF [NOT] EXIST filename命令


解决方案

使用括号,而不是报价:

  IF [%1] == [] GOTO MyLabel

<子>注:原来的答案是:结果
<子> IF(%1)==()GOTO MyLabel 结果
<子> @jeb指出,这是不安全的,并建议@Synetech替代,所以我修改了答案。

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.

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

解决方案

Use brackets instead of quotes:

IF [%1] == [] GOTO MyLabel

Note: The original answer was:
IF (%1) == () GOTO MyLabel
@jeb pointed out this was insecure, and @Synetech suggested an alternative so I revised the answer.

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

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