本文介绍了FOR DO语句中的局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
变量'var'在显示其值之前是否被销毁?
does the variable 'var' gets destroyed before displaying its value?
@echo off
FOR /f "tokens=* delims=" %%G IN ('dir') DO (
set var=%%G
echo %var%
)
推荐答案
由于解析循环体时解析了对%var%
的引用,因此该方法永远不会起作用.您必须启用延迟的变量扩展,并改用!var!
:
This will never work since the reference to %var%
is resolved when the body of the loop is parsed. You have to enable delayed variable expansion and use !var!
instead:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=* delims= " %%G IN ('dir') DO (
SET VAR=%%G
ECHO !VAR!
)
ENDLOCAL
您可以通过在命令行中键入 SET/?
来阅读全部内容;-)
You can read all about it by type SET /?
at a command line ;-)
这篇关于FOR DO语句中的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!