For Loop not updating my counter

April 1, 2010 at 12:08:38
Specs: Windows XP
I have a simple For Loop in a batch file and I can't get my counter to count. I've been looking all over and can't figure out what I'm doing wrong.

I'm just looking in a folder and running through the list of files. I want to store the file names in variables.


Here's what I have:

@echo off
setlocal enabledelayedexpansion
set /a counter=0

  for /f %%x in ('dir /b /o-d') DO (
    set /a counter+=1
    echo %counter%
    echo %%x
    set file%counter%=%%x
    )

endlocal

When it runs, the first time through it tells me
0
-filename-

The next loop it tells me:
0
-filename2-

So I can tell it's running through but the counter variable just isn't being updated. What am I missing?

I've tried counter+=1, counter=%counter%+1, counter=!counter!+1 and nothing works.

Thanks.


See More: For Loop not updating my counter

Report •

#1
April 1, 2010 at 12:25:26
!counter!


=====================================
Helping others achieve escape felicity

M2


Report •

#2
April 1, 2010 at 17:35:37
might want to clarify: NOT in the set /a statement, everywhere else. keep this, it's correct: set /a counter+=1
everywhere else, use !counter!

Report •

#3
April 2, 2010 at 03:42:11
nbrane,

yepper; didn't thinka that.

I should have said !count! instaead of %count%.


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
April 2, 2010 at 05:05:21
OK, the ! thing fixed the counter but now I'm not sure about the other part.

The line: set file%counter%=%%x

What i want to do is end up with variables called file1, file2, file3 that I can then reference outside the For loop. That part doesn't seem to be working. Can I do it that way or is there a different way that might work?


Report •

#5
April 2, 2010 at 10:46:17
for /f %%x in ('dir /b /o-d') DO (
set /a counter+=1
set file!counter!=%%x
call :xxx
)
goto :eof
:xxx
echo var is: file%counter%
echo value is: !file%counter%!

Report •

Ask Question