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

Discussion in 'Programming and Scripting' started by alfonz19, Jul 8, 2013.

  1. alfonz19

    alfonz19 New Member

    Joined:
    Jul 8, 2013
    Messages:
    2
    Likes Received:
    0
    Hi,

    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 127.0.0.1:8888 -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:
    a
    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?
     
  2. Josephur

    Josephur Windows Forum Admin
    Staff Member Premium Supporter

    Joined:
    Aug 3, 2010
    Messages:
    1,019
    Likes Received:
    125
    Never forget the reverse piping abilities...

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

    myvar should now = "blah blah"
     
  3. Josephur

    Josephur Windows Forum Admin
    Staff Member Premium Supporter

    Joined:
    Aug 3, 2010
    Messages:
    1,019
    Likes Received:
    125
    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]"
     

Share This Page

Loading...