Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Set environment variable from stdout of command

In a batch file, using sed to extract a string from a file (sed writes its output to stdout), I would like to set an environment variable to that extracted string. Is that possible?

Eg. from a Windows scheduled task xml file, which contains the line:

<spaces><Author>Papa\burger</Author><nl>

using the sed command:

sed -n -e"s/^.*<Author>\([a-zA-Z0-9_]*\)\\[a-zA-Z0-9_]*<\/Author>$/\1/ip" filename.xml

produces the output:

Papa<nl>

How to set environmentvariable=Papa?
 
One method that comes to mind is to do
echos set environmentvariable=>tempfile.btm
sed ... >>tempfile.btm
call tempfile.btm
del tempfile.btm
 
In a batch file, using sed to extract a string from a file (sed writes its output to stdout), I would like to set an environment variable to that extracted string. Is that possible?

Eg. from a Windows scheduled task xml file, which contains the line:

<spaces><Author>Papa\burger</Author><nl>

using the sed command:

sed -n -e"s/^.*<Author>\([a-zA-Z0-9_]*\)\\[a-zA-Z0-9_]*<\/Author>$/\1/ip" filename.xml

produces the output:

Papa<nl>

How to set environmentvariable=Papa?
Code:
SET environmentvariable=%@execstr[sed -n -e"s/^.*<Author>\([a-zA-Z0-9_]*\)\\[a-zA-Z0-9_]*<\/Author>$/\1/ip" filename.xml]
 
Thanks once again, Steve and Vince, for prompt and helpful replies. I had thought of using a temporary file, but that was as far as I got. But @execstr[] is perfect for the task, it does exactly what I needed (in fact, an example in the help file even shows using it to set an environment variable!).
 

Similar threads

Replies
4
Views
2K
Back
Top