Hi,
I'm reading from a file some name/value arguments (e.g. foo=bar) so that these are stored in say %%i, %%j.
I would like to just do
set %%i
echo %foo%
but it's not working. Is there a way to get the %%i to expand such that it can be used in the new set command?
Thanks
Any way to execute script within a variable?
Moderator: DosItHelp
Re: Any way to execute script within a variable?
Clarification, the bigger problem is that my input file has
foo1=bar1, foo2=bar2, ...
I want to read in that file such that I end up with variables %foo1% equal to 'bar1'.
I can use a FOR loop to split each pair but then I need to keep track of %%i, %%j, %%k, ...
I then @echo set %%i >> temp.bat
call temp.bat
but again, this means hardcoding the number of variables. It would be better if they were newline rather than comma separated but I figure there's got to be a way.
Thanks
foo1=bar1, foo2=bar2, ...
I want to read in that file such that I end up with variables %foo1% equal to 'bar1'.
I can use a FOR loop to split each pair but then I need to keep track of %%i, %%j, %%k, ...
I then @echo set %%i >> temp.bat
call temp.bat
but again, this means hardcoding the number of variables. It would be better if they were newline rather than comma separated but I figure there's got to be a way.
Thanks
Re: Any way to execute script within a variable?
droberts wrote: It would be better if they were newline rather than comma separated but I figure there's got to be a way.
Actually I think the newline separator is by far the best solution for your situation:
- read each line in its entirety into the %%i variable
- replace all commas with line feed
- use another FOR loop to break the lines and process each set command
Code: Select all
setlocal enableDelayedExpansion
::define a Line Feed (newline) string (normally only used as !LF!)
set LF=^
::Above 2 blank lines are required for LF definition - do not remove
::define a Line Feed string that can be used as %xLF%
set ^"xLF=^^^%LF%%LF%^%LF%%LF%"
for /f "delims=" %%i in (file.txt) do(
set ln=%%i
set ln=!ln:,=%xLF%!
for /f "delims=" %%C in ("!ln!") do set %%C
)
There is one major limitation with the above - the initial assignment of ln will be corrupted if the line contains any ! characters because delayed expansion is enabled. There are complicated ways to get around this within a FOR loop, but there is a much easier solution using New technic: set /p can read multiple lines from a file. This technique works as long as your file uses the Windows standard of <CR><LF> for line termination and not the Unix standard of <LF>.
Code: Select all
setlocal enableDelayedExpansion
::define a Line Feed (newline) string (normally only used as !LF!)
set LF=^
::Above 2 blank lines are required for LF definition - do not remove
::define a Line Feed string that can be used as %xLF%
set ^"xLF=^^^%LF%%LF%^%LF%%LF%"
<file.txt (
for /f "delims=" %%n in ('find /c /v "" file.txt') do set "len=%%n"&for /l %%l in (1 1 !len:*: ^=!) do (
set "ln="
set /p "ln="
if defined ln (
set ln=!ln:,=%xLF%!
for /f "delims=" %%C in ("!ln!") do set %%C
)
)
)
Dave Benham