批处理文件设置变量不工作 [英] Batch File Set Variable not working

查看:162
本文介绍了批处理文件设置变量不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个BAT文件变量的一些简单的设置。它不设置变量。根本就没有什么奇怪的结构,它使用相同的变量名简单的变量替换。我BAT文件减少到概念版的一个简单证明:

I'm doing some simple setting of a variable in a BAT file. It's not setting the variable. There aren't any odd constructs, it's simple variable substitution using the same variable name. I reduced the BAT file to a simple proof of concept version:

set TESTVAR = "No Value"
ECHO var = %TESTVAR%
set TESTVAR = ""
ECHO var = %TESTVAR%
set TESTVAR = "New value"
ECHO var = %TESTVAR%

和输出显示,没有一个SET命令似乎工作。什么鬼我在这里失踪。我已经写BAT文件多年,我从来没有见过这个。下面是运行此测试的输出:

And the output shows that none of the SET commands seem to be working. What the heck am I missing here. I've been writing BAT files for years and I've never seen this before. Here's the output from running this test:

C:\Users\rs02130\Desktop>test

C:\Users\rs02130\Desktop>set TESTVAR = "No Value"

C:\Users\rs02130\Desktop>ECHO var =
var =

C:\Users\rs02130\Desktop>set TESTVAR = ""

C:\Users\rs02130\Desktop>ECHO var =
var =

C:\Users\rs02130\Desktop>set TESTVAR = "New value"

C:\Users\rs02130\Desktop>ECHO var =
var =
C:\Users\rs02130\Desktop>

我希望第一和第三ECHO命令显示的值没有价值和新价值。到底是什么回事?

I expect the first and third ECHO commands to display the values "No Value" and "New value". What the heck is going on?

推荐答案

问题是等号周围的空间。这应该做你想要的。

The problem is the spaces around the equal sign. This should do what you want.

set TESTVAR="No Value"
ECHO var = %TESTVAR%
set TESTVAR=""
ECHO var = %TESTVAR%
set TESTVAR="New value"
ECHO var = %TESTVAR%

这篇关于批处理文件设置变量不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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