Results 1 to 10 of 10
  1. #1
    2 Star Lounger
    Join Date
    Aug 2014
    Posts
    123
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Add command output to a variable

    Here is what I am trying to replicate from about 4 different discussions on this:
    find /v /c "" TODO.txt > tst.txt
    produces:
    ---------- TODO.TXT: 33 in that file
    I want to put this into a variable with this:
    set /p var=<tst.txt
    echo %var%
    OR
    type tst.txt | set /p var=
    OR
    <tst.txt (set /p var=)
    but var when echo-ed: echo %var%
    always prints %var%
    OR
    find /v /c "" TODO.txt | set /p var=

    I know some of these I could not get their examples to work either but included them to eliminate them from possibles.
    Does anyone have a working command sequence to put the file's output
    into a variable.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Spark,

    What is your endpoint for this procedure?
    Could you provide a test TODO.txt file?
    Would you consider using PowerShell (I'm pretty sure this can be done with PS).

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 986 Times in 916 Posts
    Getting text into a batch file is easy - when you know how.

    for %x in (tst.txt) echo %x

    cheers, Paul

  4. #4
    2 Star Lounger
    Join Date
    Aug 2014
    Posts
    123
    Thanks
    19
    Thanked 0 Times in 0 Posts
    @PaulT: THanks 4 that, may use it if I cannot find how others have used the set command. It seems like it should work but I could not get it to.
    @RetiredGeek: the TODO.txt file is my grocery list from which the find command found 33 lines. For some reason the post was truncated by the system and my connection being trashy I did not edit. The output of find /v /c "" ... is:
    ========== TODO.TXT: 33
    My need is to capture everything beyond the ":" which I can do but the method of capturing the line in a variable is what I need and simultaneously need to find why so many were able to use the set command to do it but I can't.
    PS
    Went back to my source for "set" and here is what I did after putting the line "--... : 33" into a file:
    verified file by typing it.
    set /p var=<tst.txt but echo %var% outputs %var% not the line or any part thereof.
    Last edited by spark55; 2015-11-27 at 22:19.

  5. #5
    2 Star Lounger
    Join Date
    Aug 2014
    Posts
    123
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Help! Is this a set bug??

    I created the following test script because I cannot get consistent results (I think). Tests 1 and 4 seem to work. Test 3 is not appending the second line to the tst.txt file. It appears that 2 lines are required for the tst.txt file to use the set command. Is this a bug?? I have used set before when reading in values with no problem. I appreciate anyone checking!
    @echo off
    cls
    ::THIS WORKS
    echo TEST 1
    vol c: > tst.txt
    type tst.txt
    set /p DATA=<tst.txt
    echo ex1 DATA = %DATA%
    echo.
    ::THIS DOESN'T
    echo TEST 2
    find /v /c "" TODO.txt > tst.txt
    type tst.txt
    set /p BABA=<tst.txt
    echo ex2 BABA = %BABA%
    echo.
    echo TEST3
    echo this is line two >> tst.txt
    type tst.txt
    set /p AAAA=<tst.txt
    echo ex3 AAAA = %AAAA%
    echo.
    echo TEST 4
    echo line1 of 2 > tst.txt
    echo line1 of 2 >> tst.txt
    type tst.txt
    set /p F4F4= < tst.txt
    echo ex4 F4F4=%F4F4%
    echo.
    echo TEST 5
    find /v /c "" TODO.txt>tst.txt
    echo here is line two>>tst.txt
    type tst.txt
    set /p avar= <tst.txt
    echo ex5 avar = %avar%
    goto :EOF

  6. #6
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 986 Times in 916 Posts
    Test 1 only stores one line of the two in tst.txt.
    2 doesn't work because the input method you chose doesn't cope with the data in the file.
    3 gives you two lines in the file but only one in the variable. This is expected as you can't have 2 lines in a variable.

    What is wrong with using the standard method of reading files, "FOR"? This will cater for all the lines you have.

    cheers, Paul

  7. #7
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 986 Times in 916 Posts
    for /f "tokens=3 delims=:" %x in ('find /c /v "" todo.txt') echo %x

    This splits the output from the find command at the delimiter, colon character, and selects the part after the 2nd delimiter.

    cheers, Paul

  8. #8
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,420
    Thanks
    33
    Thanked 195 Times in 175 Posts
    Quote Originally Posted by spark55 View Post
    Does anyone have a working command sequence to put the file's output
    into a variable.
    Let me confirm what you are attempting to do:
    are you trying to put several lines of text into a single environment variable in a BATch file?

    If this is the case, I would ask "Why?"
    You will hit some sort of limit (be in 2047 or 32767) in the number of characters, and in coping with line endings, CR/LF. 0x0d0a, or however you want to represent them. If you even manage to achieve this, you will find it extremely difficult to manipulate.

    With a problem like this it's a good idea to state what you are actually trying to achieve, and only then to say what you have tried so far!
    BATcher

    Time prevents everything happening all at once...

  9. #9
    2 Star Lounger
    Join Date
    Aug 2014
    Posts
    123
    Thanks
    19
    Thanked 0 Times in 0 Posts
    @PaulT: Like I said in my post#2 I want to find why "set" is not functioning as I have seen shown elsewhere.
    U probably noted that in #1&#3 it accepts only the 1st line if 2 provided but never if only 1 line.
    I would have expected the last line read to be the one accepted.
    @BATcher:Sorry for the lack of elaboration.
    The goal is to send 1 line to the set command's variable as that is what has been implied by 3 other citations. I have had no luck in finding documentation on this usage think it will be useful in the future if I can get it. The set command when assigned a variable directly accepts that but with this usage requires 2 lines to get the 1st one and to me makes it anomalous.
    The direct answer to your question is no just 1 line to the variable without using 2 lines.
    No CR is required when assigning a value directly so why is one required when inputting a string? and why 2 lines (1 CR is apparently required to input 2 strings (untested). Wonder how 2 adjacent CR is treated?
    My objective is to extract all after the ":".
    Thanks again for all the input

    PS--It seems like my connection/post can get misinterpreted if my middle digit clicks the right button while the left button is engaged.
    Is this to be expected? It has happened twice.
    @Ret'd Geek: PS IMHO is a tired
    laborious attempt to emulate "bash" which is available for Windows anyway.
    I looked at it when it came out with W8.1 and discarded it as worse than DOS Batch-an abomination. I know M$ is in favor of trashing anything Not Invented Here.
    I interviewed for a Linux SA position there and turned it down before it was even offered -impossible to work in those conditions.
    Last edited by spark55; 2015-11-29 at 02:44.

  10. #10
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 986 Times in 916 Posts
    SET doesn't work as you expect because using SET that way is inconsistent, that's why most people use FOR.
    Post #7 shows how to get the data after the colon.

    cheers, Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •