einstein1969 wrote:hi to all,
i have few questions for this code:
Code: Select all
@echo off
echo(|more|(
for /l %%k in (1,1,100000) do (
(if %%k==5 exit)
echo %%k
)
)
echo end
seem that execute the for /L without count to 100000 and not exit from current context.
It is safe?
Why the @echo off don't work?
einstein1969
When I saw this code I thought it may be used to implement a simpler
while loop, so I made several tests. For example:
Code: Select all
@echo off
setlocal
set /A index=0, sum=0
echo/|(@echo off
for /L %%? in (1,0,1) do (
set /A index+=1, sum+=index > NUL
echo !index!
if !index! == 10 exit !sum!
)
)
echo Returned value: %errorlevel%
There is, however, a point that can not be solved in this code: to
programatically enable Delayed Expansion in the FOR loop in order to access modified values. If the "FOR /L" is executed via "CMD /V:ON", then the EXIT command also terminate the original command-line window. The only way to achieve this is using REGEDIT.EXE to modify this value in the registry:
Code: Select all
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion = 0x1
After that, previous code run correctly:
Code: Select all
C:\> While.bat
1
2
3
4
5
6
7
8
9
10
Returned value: 55
A last point: if I used "EQU" instead of "==" in the IF command, an error about "10 unexpected at this time" is issued. I don't understand why this happen, because Extensions are enabled by default even in the command-line context!
Antonio