windows 批处理文件 - 使用变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1388247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:05:39  来源:igfitidea点击:

batch file - use of variable

windowsbatch-file

提问by Joey

@echo off
set filename = 
cd GWConfig_TDS-mtpe3003
set filename = VCU17_CCU6\applications\VCU17APP 
GOTO CHECKFILE

:CHECKFILE 
echo reached
IF EXIST %filename% ( echo exists 
) ELSE ( echo Doesnot exist )

/////////////////////////////////////////////////

/////////////////////////////////////////////////

Here output shows :

这里的输出显示:

reached

Doesnot echo "exists" or "Doesnot exist"

不回显“存在”或“不存在”

Is there anything wrong with the use of variable "filename".

使用变量“文件名”有什么问题吗?

Also,

还,

@echo off
set filename = 
cd GWConfig_TDS-mtpe3003
set filename = VCU17_CCU6\applications\VCU17APP 
GOTO CHECKFILE

:CHECKFILE 
echo reached
IF EXIST VCU17_CCU6\applications\VCU17APP ( echo exists 
) ELSE ( echo Doesnot exist )

gives output:

给出输出:

reached
exists.

回答by Jon Skeet

There are two problems here. One is the space after the variable name:

这里有两个问题。一种是变量名后的空格:

SET filename = whatever

should be

应该

SET filename=whatever

(Or you could use %filename %later, but that's just horrible :)

(或者你可以%filename %稍后使用,但这太可怕了:)

The second problem is that without any quotes, your "IF" test won't work properly if %filename%is empty. Quote it instead:

第二个问题是,如果没有任何引号,您的“IF”测试如果%filename%为空将无法正常工作。改为引用它:

IF EXIST "%filename%" ( echo exists
) ELSE ( echo Doesnot exist )

回答by Joey

I don't have the time to properly re-create this now, but I can spot a few potential problems:

我现在没有时间正确地重新创建它,但我可以发现一些潜在的问题:

  • Have you tried removing the spaces around the =in set:

    set filename=VCU17_CCU6\applications\VCU17APP
    

    otherwise you might have a single space leading your file name

  • You might want to try to quote the use of %filename%:

    IF EXIST "%filename%" ( ...
    
  • 您是否尝试过删除=in周围的空格set

    set filename=VCU17_CCU6\applications\VCU17APP
    

    否则你的文件名前可能会有一个空格

  • 您可能想尝试引用以下用法%filename%

    IF EXIST "%filename%" ( ...