Board logo

标题: [请教]变量延迟的应用为什么失败 [打印本页]

作者: zhaxi     时间: 2006-6-27 22:38    标题: [请教]变量延迟的应用为什么失败
@echo off
echo.
echo.
echo 192.168.10.16上有以下最新的服务端供下载:
dir \\192.168.1.1\rel_inc(增量发布流)\svr /ON /A-D /B /-D /P >%zhx%\Temp\earlist.txt

setlocal ENABLEDELAYEDEXPANSION
set /a num=1
for /f "tokens=1 delims= " %%i in (%zhx%\Temp\earlist.txt) do (set file=%%i
set /a num+=1
echo ◇%num% %%i )

endlocal

echo.
pause

------------------------------------------------------------------

我想通过上述语句实现这样的显示结果:
◇1 a.txt
◇2 b.txt
◇3 c.txt
◇4 d.txt

但我实际执行结果却是:
◇1 a.txt
◇1 b.txt
◇1 c.txt
◇1 d.txt

也就是说明变量num没有延迟扩展,而是for语句预读时就赋予了1。

请高手帮忙改正。谢谢。

Last edited by zhaxi on 2006-6-27 at 22:41 ]

作者: zhaxi     时间: 2006-6-27 22:58
晕。刚才查了半天。是不是由于我应该把%num%改成 !num! 啊。

哈哈。

作者: bagpipe     时间: 2006-6-28 08:54
@echo off
setlocal
for /f "delims=" %%a in (1.txt) do (
set /a aa+=1
call :hi %%a
)
endlocal
goto :eof
:hi
echo %aa% %*
goto :eof


有的时候也可以不用延迟环境变量来实现的..........

Last edited by bagpipe on 2006-6-28 at 11:17 ]

作者: zhaxi     时间: 2006-6-28 12:51
还有这种做法啊。谢谢大哥了。通过一个call跳到for外面就可以实现aa的重新赋值了啊。

作者: ruisoft     时间: 2007-6-11 15:09
通过一个call跳到for外面后如何跳回for???

作者: stornager     时间: 2007-6-11 15:45
看3楼的代码,call本来就在for内。

作者: ieutk     时间: 2007-6-11 23:45
Originally posted by ruisoft at 2007-6-11 03:09 PM:
通过一个call跳到for外面后如何跳回for???



goto :eof

作者: wufengseu     时间: 2008-1-8 22:01
this can be done as follows:

@echo off

echo.
echo.
echo.

setlocal ENABLEDELAYEDEXPANSION

set /a num=0
for /f "tokens=1 delims= " %%i in (1.txt) do (set file=%%i
set /a num= num-1
echo ◇!num! %%i.txt )

endlocal

echo.
pause

Last edited by wufengseu on 2008-1-8 at 10:02 PM ]