TonyStark
TonyStark

Reputation: 96

How to access an environment variable in a FOR loop set in same FOR loop?

This is what I have:

FOR /f %%G IN (school\students\%studentname%\classes.txt) DO (
    set /p grade=<"school\students\%studentname%\classes\%%G\grade.txt"
    pause
    echo %grade%
    echo You Have %grade% in %%G
)
pause

My problem is that I get back:

You have  in Math.
You have  in English.

In the grade.txt files I have an A in one and a B in the other. The %%G is set to English and Math throughout the FOR command and find the folders fine. But the %grade% variable is not setting for some reason. I cannot figure out why. Thanks

Upvotes: 0

Views: 584

Answers (3)

sjoy
sjoy

Reputation: 502

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

Upvotes: 0

npocmaka
npocmaka

Reputation: 57252

You need delayed expansion :

@echo off

set "student=school\students\%studentname%\classes.txt"

setlocal enableDelayedExpansion
FOR /f %%G IN (%student%) DO (
 set /p grade=<%student%
  pause
 echo !grade!
 echo You Have !grade! in %%G
)
 pause

endlocal

but grade will be always the same - set /p grade=<file reads only the first line of the file. And %%G will only access the first word of the file.Check the tokens and delims options.

Upvotes: 1

Faron
Faron

Reputation: 1243

Perhaps you would have better result if you used " " while working with echo ? i.e. echo "$VARIBALE"

Upvotes: 0

Related Questions