Pipe Output into Environment Variable?

Discussion in 'Microsoft Windows 2000 CMD Promt' started by Claus Koch, Aug 11, 2004.

  1. Claus Koch

    Claus Koch Guest

    Hi there

    i am looking for a way to pipe output of any command into a environment
    variable under w2k or XP.

    i don't want to go via a redirection by putting the Output into a text file
    first etc ...

    for example i want to search in a text file an put the result in a variable

    "findstr seachtext c:\testfile.txt"

    how can i put the result in a variable with the least effort ....

    thx in advance
     
    Claus Koch, Aug 11, 2004
    #1
    1. Advertisements

  2. "Claus Koch" wrote:

    >Hi there
    >
    >i am looking for a way to pipe output of any command into a environment
    >variable under w2k or XP.
    >
    >i don't want to go via a redirection by putting the Output into a text file
    >first etc ...
    >
    >for example i want to search in a text file an put the result in a variable
    >
    >"findstr seachtext c:\testfile.txt"
    >
    >how can i put the result in a variable with the least effort ....
    >
    >thx in advance
    >

    The intrinsic way of doing this is with a for loop.
    (works also with a temp file, but you don't have to care about)

    At the command line:
    for /f "delims=" %A in ('findstr searchtext') do @set YourVar=%A
    In a batch:
    for /f "delims=" %%A in ('findstr searchtext') do set YourVar=%%A

    If there is more than one findstr result, the last one is set.

    The free tool conset from Frank P. Westlake can do it this way:
    conset YourVar=`findstr searchtext`
    But the result may be a multiline var :)

    Hosted now by Ted Davis at http://gearbox.maem.umr.edu/fwu/

    HTH
    --
    Greetings
    Matthias________________________________________
    For help on nt commands enter in a cmd window:
    W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
     
    Matthias Tacke, Aug 11, 2004
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.