Any way to execute script within a variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
droberts
Posts: 2
Joined: 04 Oct 2011 16:55

Any way to execute script within a variable?

#1 Post by droberts » 04 Oct 2011 16:58

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

droberts
Posts: 2
Joined: 04 Oct 2011 16:55

Re: Any way to execute script within a variable?

#2 Post by droberts » 04 Oct 2011 17:48

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Any way to execute script within a variable?

#3 Post by dbenham » 04 Oct 2011 21:44

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

Post Reply