Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: For Loop Problems  (Read 2693 times)

0 Members and 1 Guest are viewing this topic.

stebo0728

  • Guest
For Loop Problems
« on: August 21, 2006, 08:29:52 PM »
Ok, I am trying to write a batch file that will make a MS Word document, and I have 99% completed, the document is a list of tracks in a directory, and serves as a label for a CD, my only problem, is that I have a FOR IN DO loop, in which the file name of each track, minus the extension is written to the file, works perfectly, however, I also have an iterator variable in the loop that I += each time to increment, this allows the track number to be written to the file, here is an example of the code I am trying to write, and the problem with it is that when I access the index variable, it writes the pre FOR loop value, not the new += value that is assigned in the FOR loop, any suggestion?!?

::Sample code
SET index=0
FOR %%i IN ("C:\<directory>\*.mp3) DO (
SET /A index += 1
ECHO %index%. %%~ni >> <docFileName>.doc
)
ECHO %index%
EXIT

This code works fine it outputs each new %%~ni with no problems, however, when %index% is accessed each time, the value 0 is always written
When the %index% is accessed the last time by the ECHO line, outside the FOR loop, the proper value is outputed, is there anything I need to do to "update" let say, the index variable value within the for loop so that the proper new value will be outputed?

uli_glueck

  • Guest
Re: For Loop Problems
« Reply #1 on: August 22, 2006, 12:29:56 AM »
Using a subvariable fixes the set problem:

SET index=0
FOR %%i IN ("C:\<directory>\*.mp3) DO call :sub %%i


:goto :EOF
:sub %%i
REM %%i is now %1
REM I never tried the qualifiers in a sub
REM  %%~ni should be something like %%~n1; no guarantee...
SET /A index += 1
ECHO %index%. %%~ni >> <docFileName>.doc
ECHO %index%

:EOF

hope it helps
uli