Board logo

标题: 在for语句中修改变量的问题 [打印本页]

作者: hahaaj     时间: 2008-4-11 11:28    标题: 在for语句中修改变量的问题

set motFn=

for /f "eol=[ usebackq" %%i in (`type %confFn%`) do (
        set a=%%i
        setlocal EnableDelayedExpansion
        if "!a:~0,4!"=="motF" set motFn=!a:~5!&echo !a:~5!   //此处echo得预期结果
        endlocal
)

echo %motFn% //此处echo则得 %motFn%变量为空??
作者: bat-zw     时间: 2008-4-11 11:40
因为在endlocal命令后所有前面的变量赋值都将取消,所以%monFn%会为空。
把if "!a:~0,4!"=="motF" set motFn=!a:~5!&echo !a:~5!改为if "!a:~0,4!"=="motF" set motFn=!a:~5!&echo !motFn!就是的啊。
作者: hahaaj     时间: 2008-4-11 12:07
我要在for后面使用for中设置的变量的值,要怎么做呢?谢谢
作者: bat-zw     时间: 2008-4-11 12:10
用call或用goto调出来啊
作者: bat-zw     时间: 2008-4-11 12:11
if "!a:~0,4!"=="motF" set motFn=!a:~5!&echo !a:~5!改为if "!a:~0,4!"=="motF" set motFn=!a:~5!&call :lp !!motFn!!

if "!a:~0,4!"=="motF" set motFn=!a:~5!&echo !a:~5!改为if "!a:~0,4!"=="motF" set motFn=!a:~5!&goto lp
作者: hahaaj     时间: 2008-4-11 14:44
我实际的代码如下

set motFn=        
set gcFn=   
set obFn=

for /f "eol=[ usebackq" %%i in (`type %confFn%`) do (
        set a=%%i
        setlocal EnableDelayedExpansion
        if "!a:~0,4!"=="motF" set motFn=!a:~5!
        if "!a:~0,4!"=="gctF" set gcFn=!a:~5!
        if "!a:~0,4!"=="binF" set obFn=!a:~5!
       endlocal
)
if "%motFn%"=="" (echo ~FAIL~~,.~ Did NOT specify motF&goto :conf)
if "%gcFn%"=="" (echo ~FAIL~~,.~ Did NOT specify gctF&goto :conf)
if "%obFn%"=="" (echo ~FAIL~~,.~ Did NOT specify binF&goto :conf)

就是要在一条for语句中给三个变量赋值,楼上的方法只能给一个变量赋值
作者: bat-zw     时间: 2008-4-11 17:36    标题: 试试这样可以不啊:

set motFn=        
set gcFn=   
set obFn=

for /f "eol=[ usebackq" %%i in (`type %confFn%`) do (
        set a=%%i
        setlocal EnableDelayedExpansion
        if "!a:~0,4!"=="motF" set motFn=!a:~5!&call :lp1
        if "!a:~0,4!"=="gctF" set gcFn=!a:~5!&call :lp2
        if "!a:~0,4!"=="binF" set obFn=!a:~5!&call :lp3
       endlocal
)
:lp1
if "%motFn%"=="" (echo ~FAIL~~,.~ Did NOT specify motF&goto :conf)
goto :eof
:lp2
if "%gcFn%"=="" (echo ~FAIL~~,.~ Did NOT specify gctF&goto :conf)
goto :eof
:lp3
if "%obFn%"=="" (echo ~FAIL~~,.~ Did NOT specify binF&goto :conf)
goto :eof
作者: bat-zw     时间: 2008-4-11 17:39
或者干脆去掉endlocal这句,把后面的%改成!
作者: hahaaj     时间: 2008-4-14 14:03


  Quote:
Originally posted by zw19750516 at 2008-4-11 05:36 PM:
set motFn=        
set gcFn=   
set obFn=

for /f "eol=[ usebackq" %%i in (`type %confFn%`) do (
        set a=%%i
        setlocal EnableDelayedExpansion
        if "!a:~0,4! ...

用call方法一跳出for语句就回不去哒。.


最后我是像楼上说的那样去掉了for结尾处的endlocal语句(不过后面引用变量还是用的“%”,没有改成“!”)。这样bat就能按照预期的方式工作了。

后来知道了setlocal EnableDelayedExpansion。。。。endlocal块中设置的变量在endlocal后就失效了。

那么。又有一个疑问:endlocal是多余的一样。。
作者: bat-zw     时间: 2008-4-14 15:15
endlocal在for语句递归层小于33的时候可以不加,但当递归层达到33以上时就必须加上了,运行下面的代码你就会明白了:
@echo off
for /l %%i in (1,1,33) do (
    set a=%%i
    setlocal enabledelayedexpansion
    echo !a!
)
pause

作者: terse     时间: 2008-4-14 15:49
我觉得可以CALL出的:
@echo off
for /f "eol=[ usebackq" %%i in (`type %confFn%`) do call:lp %%i
echo %motFn% %gcFn% %obFn%
pause
goto :eof
:lp
set a=%1
        if "%a:~0,4%"=="motF" set motFn=%a:~5%
        if "%a:~0,4%"=="gctF" set gcFn=%a:~5%
        if "%a:~0,4%"=="binF" set obFn=%a:~5%