批处理文件:SET变量=字符串不起作用 [英] Batch file : SET variable=string doesn't work

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

问题描述

我有此代码:

if %Ret:~6,4% EQU %Year% (
SET test=text
ECHO %test%
) else (
ECHO NO
)

代码进入if循环,但始终返回Echo关闭! 我注意=前后的空格. 有什么想法吗?

The code enters in the if loop but it returns always Echo is off! I've pay attention to the space before and after the =. Any ideas?

推荐答案

今年此类问题的编号#342.

Number #342 of this type of question this year.

在执行任何行之前,解析一个块时,扩展百分比就会增加.
因此,echo %test%会在设置变量之前展开.

Percent expansion occours when a block is parsed, before any line is executed.
So the echo %test% is expanded before the variable is set.

因此存在延迟扩展,该延迟扩展在执行一行时扩展.

Therefor exists the delayed expansion, which expands when a line is executed.

setlocal EnableDelayedExpansion
if "%Ret:~6,4%" EQU "%Year%" (
  SET test=text
  ECHO !test!
) else (
  ECHO NO
)

这篇关于批处理文件:SET变量=字符串不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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