1.

Solve : For Loop Problems?

Answer»

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:\\*.mp3) DO (
SET /A index += 1
ECHO %index%. %%~NI >> .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?Using a subvariable fixes the set problem:

SET index=0
FOR %%i IN ("C:\\*.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 >> .doc
ECHO %index%

:EOF

hope it helps
uli



Discussion

No Comment Found