Windows 7 basic scripting, usage piping, output redirections and variable in one example.


I mostly use another OS, and need to rewrite following script into one which works in windows.

OK, here's the command:
curl -s -x -b ./cookies.txt -X GET http://localhost:8080/api/attachment/list 2>&1 | tr -d '[]' | sed "s/,/\n/g" | head -n 1 | sed "s/.\(.*\)./\1/"

here's what it does: loads content of some URL using curl and redirect output 2 to std one. This return json like: '["a", "b", "c"]' this result is piped to following commands resulting to single line output:
Commands are little bit crazed, because tr does not work with \", i don't know why. *But* given command *works*. What I need to do, is to store its result to variable. (need to be used later).I've tried to do:
set myVariable=...
but that stores empty string into it instead of valid value.
I've found some workaround using for cycle, but that did not liked redirection of output.

Does anybody know solution(even really complicated one) to this simple problem?


Windows Forum Admin
Staff member
Premium Supporter
Never forget the reverse piping abilities...

echo blah blah > tmpFile
set /p myvar= < tmpFile
del tmpFile

myvar should now = "blah blah"


Windows Forum Admin
Staff member
Premium Supporter
You can also use a for statement...

for /f "delims=" %a in ('ver') do @set myvar=%a
echo %myvar% will return the output.. in this case for me "Microsoft Windows [Version 6.1.7601]"

This website is not affiliated, owned, or endorsed by Microsoft Corporation. It is a member of the Microsoft Partner Program.