Incremental count in a FOR loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
panjabi
Posts: 3
Joined: 07 Apr 2008 12:18

Incremental count in a FOR loop

#1 Post by panjabi » 07 Apr 2008 12:24

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?

jaffamuffin
Posts: 40
Joined: 25 Jan 2008 14:05

#2 Post by jaffamuffin » 07 Apr 2008 17:21

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
)

panjabi
Posts: 3
Joined: 07 Apr 2008 12:18

Thanks JaffaMuffin

#3 Post by panjabi » 07 Apr 2008 23:53

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
)

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#4 Post by DosItHelp » 08 Apr 2008 08:19

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:

jaffamuffin
Posts: 40
Joined: 25 Jan 2008 14:05

#5 Post by jaffamuffin » 08 Apr 2008 15:43

Nice tip. Any way to avoid SETLOCAL ENABLEDELAYEDEXPANSION is good :)

Post Reply