Page 1 of 1

Incremental count in a FOR loop

Posted: 07 Apr 2008 12:24
by panjabi
Hope you can help, trying to increment a count inside a for loop.

see below.

for %%F in (%D%) do (
set /a H=%E%
set /a H=%H%+1
echo."AtomicParsley.exe" "%%F" --stik "TV Show" --TVShowName "%T% Season %S%" --TVSeason %S% --TVEpisodeNum %H% --artist "%T% Season %S%" --writeBack>>F:\ipodTvShow\BAT_Scripts\test.txt
)

cannot get the counter VAR=H to increment :x

any ideas?

Posted: 07 Apr 2008 17:21
by jaffamuffin
Looks like part of a nested for loop? To get variables to evaluate inside a for loop you need to enable delayed expansion and use ! instead of % to indicate the variable to expand when the interpreter gets to it, rather than at the opening of the for statement.

Try something like this.

Code: Select all


SETLOCAL ENABLEDELAYEDEXPANSION
...
...
...
...
for %%F in (%D%) do (
set /a H=%E%
set /a H=!H!+1
echo."AtomicParsley.exe" "%%F" --stik "TV Show" --TVShowName "%T% Season %S%" --TVSeason %S% --TVEpisodeNum !H! --artist "%T% Season %S%" --writeBack>>F:\ipodTvShow\BAT_Scripts\test.txt
)

Thanks JaffaMuffin

Posted: 07 Apr 2008 23:53
by panjabi
That's sorted it had to take out one of the lines which kept setting H to E, then worked fine.

now looks like.

for %%F in (%D%) do (
set /a E=!E!+1
echo.!E!>>F:\ipodTvShow\BAT_Scripts\test.txt
echo."D:\My Downloads\ipodTvShow\AtomicParsley.exe" "%%F" --stik "TV Show" --TVShowName "%T% Season %S%" --TVSeason %S% --TVEpisodeNum !E! --artist "%T% Season %S%" --writeBack>>F:\ipodTvShow\BAT_Scripts\test.txt
)

Posted: 08 Apr 2008 08:19
by DosItHelp
Just a tip:
The "set /a" command can detect variables in a formular so it works without turing on delayed expansion:

Code: Select all

for %%F in (%D%) do ( 
    set /a E=E+1
    ...
)

or short:

Code: Select all

for %%F in (%D%) do ( 
    set /a E+=1
    ...
)

Hope this helps :wink:

Posted: 08 Apr 2008 15:43
by jaffamuffin
Nice tip. Any way to avoid SETLOCAL ENABLEDELAYEDEXPANSION is good :)