The following will work if I understand your folder structure correctly. Like this:
- students\name1\class1
- . . . . . . . .\ . . . . . . \class2
-
. . .
- students\name2\class1
- . . . . . . . .\ . . . . . . \class2
. . . and that classes.txt is in the name folder and a grade.txt file is in each class folder.
@echo off
set /p studentname="Enter student name: "
set studentpath=C:\school\students\%studentname%
For /f %%G in (%studentpath%\classes.txt) do (
For /f %%i in (%studentpath%\%%G\grade.txt) do (
echo %studentname% got %%i in %%G
)
)
pause
You need to set the correct studentpath.
The outer loop reads a class from classes.txt and assigns the value (class name) to %%G. Then the inner loop runs to get the grade for this class, which is assigned to %%i. The result is echoed and we go back to the outer loop for the next class. This gets around the issue of DelayedExpansion (explained here) by not setting any variables inside the for loops, but simply using the parameters %%G and %%i for the output.
Be aware that this setup of nested loops only works because the inner loop only needs to loop one time--to go fetch the single grade for that single class from grade.txt.
With "nested for loops, for each iteration of the outer loop all iterations of the inner for are executed." [@MCND]. So, for example, if grade.txt contained the grades for both classes, each on a separate line, like this:
. a
. b
the result would be:
. studentame got a in math
. studentname got b in math
. studentame got a in english
. studentname got b in english