|
Answer» Hi! I have a for loop in a *.bat file that looks something like:
Code: [Select]SET ENVVARLIST=env-vars-list.txt FOR /F "tokens=1,* delims==" %%A IN (%ENVVARLIST%) DO ( SETX %%A "%%B" ) and env-vars-list.txt looks something like: Code: [Select]LIB_ROOT=%~dp0%mylib it basically has a bunch of environment variables with their paths The For loop doesn't seem to process the %~dp0% if I put the command in the For loop it works, but since the environment variables all have different paths it's a bit inconvienient.
Does anyone have a fix for this?Well for starters your SETX command has no equals sign. It is removed from your text file in the FOR command because you specified it as a delimiter.This should solve your expansion problem Code: [Select]@echo off SET ENVVARLIST=env-vars-list.txt FOR /F "tokens=1,* delims==" %%A IN (%ENVVARLIST%) DO ( call SETx %%A="%%B" ) But why bother using the equals sign as a delimiter. Use it to your advantage. Code: [Select]@echo off SET ENVVARLIST=env-vars-list.txt FOR /F "delims=" %%A IN (%ENVVARLIST%) DO ( call SETX %%A )Thank you for the reply! There is a very subtle syntax difference between SET and SETX. Set uses an =, but SETX does not use one. I believe that part of my syntax was correct, but I may be missing the interpretation of your comment...
How do you get the command to parse the %~dp0% when it is read from an external file? when it reads it from the file it doesn't convert it to the current drive and path information... it's treated as a STRING literal in the environment table.
i.e. I'm hoping that %~dp% gets converted to something like: C:\Users\MyName\Documents\Visual Studio xxx\Projects\
testing it with the Call command.... hope that worksNo use of the call command didn't help with the parsing. Any other SUGGESTIONS?Quote from: remstadt on March 31, 2014, 01:08:42 PM No use of the call command didn't help with the parsing. Any other suggestions?
Let me see your code. It has to work. The %%A and %%B are expanded first even before the call statement run. The CALL statement then starts its own environment and expands the %dp0 variable.
Your code should look like this. Code: [Select]@echo off SET ENVVARLIST=env-vars-list.txt FOR /F "tokens=1,* delims==" %%A IN (%ENVVARLIST%) DO ( call SETX %%A "%%B" ) But for testing purposes I am using this. Code: [Select]@echo off SET ENVVARLIST=env-vars-list.txt FOR /F "tokens=1,* delims==" %%A IN (%ENVVARLIST%) DO ( call SET %%A=%%B set lib_ )When I run it with SETX this is the output I get. Code: [Select]SUCCESS: Specified value was saved.And now when I OPEN up a cmd prompt it is now a static environmental variable. Code: [Select]H:\>set lib_ LIB_ROOT=C:\BatchFiles\environ\mylibAnd you should ALSO be aware of this if you are trying to use that new variable in your current batch file. From the SETX help file QuoteNOTE: 1) SETX writes variables to the master environment in the registry.
2) On a local system, variables created or modified by this tool will be available in future command WINDOWS but not in the current CMD.exe command window. Hah! you were right all along, I just needed to open a new window. Thanks!
|