Windows batch scripting and variable expansion

I am attempting to set an env variable in the batch script and read in it's expansion from an external file, then print the value.

--batch file -----
set X=Z:

for /f %%i in (line.txt) do @set VAR=%%i

echo %VAR%

--line.txt -----

--As output -----
I am wanting to see Z: echo to the command window.  With what I have above, it will currently echo %X%, but I need that to expand also.

Who is Participating?
Your problem is that the command interpretor never actually sees what's being read from line.txt as anything other than data, i.e., it doesn't expand %%i beyond the single level.

This sort of thing, however, will get you what you want:

for /f %%i in (line.txt) do (
echo set VAR=%%i >aaa.bat
call aaa.bat
del aaa.bat
mouse050297Author Commented:
I guess I should have added a little more of what I want to do once the variable is expanded.  I need to expand it within the original batch file.  Calling a secondary batch file is appropriate for my original question and I can reference it accordingly.  Here is what I am trying to do.  I am given the string
I break this into two files, $(X) into line.txt and \msdos.sys into line2.txt

I use character replacement on line.txt to change the contents to %X%

I read in and expand the %X%, then append the contents from line2.txt to create an object string.  Then "dir" the object to check for existence, if exist, append it to an additional line_final.txt, if not exist, then do nothing with the object string and read in the next line from the original.txt file and parse accordingly as above.  

As I write this, I see that your solution should work for what I am trying to do.  I will wait to see if you have any additional suggestions for a solution now that you know the rest of the story.
mouse050297Author Commented:
Forgot that this was still open.  Points are yours.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.