0

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I should have been a little more clear. Each class has a folder and in it is a grade.txt file. So I wanted %grade% to set to the grade in the text file of each class folder. The class folder to look in I wanted to be determined by the class name in the classes.txt file and then put in the path for the class folder as %%G then %%H an so on. So that each class in classes.txt would be looked in their folders for grade.txt and then the grade echoed. I hope that makes sense. Unless delayed expansion would solve this. But I have no idea what delayed expansion is. Sorry for the lengthy explanation...
I also tried to use your answer above but it produced the same result. @npocmaka
@TonyStark will be easier for you to explain with example
0

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

2 Comments

The above answer did not work, but your original response worked perfectly, with the nested for loops. Thanks again @sjoy And if you could add it back that would be great.
OK @TonyStark. Rolled back to previous answer. Surprised the set /p method for reading file didn't work since I test everything, but great that this does what you need.
0

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

1 Comment

No this did not produce a better result. All I got was You have "" in Math

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.