1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

"History" command not giving me what I want when executed by @Exec... functions...

Discussion in 'Support' started by mathewsdw, Jan 10, 2012.

  1. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    I debated somewhat before writing this posting whether I should post the whole situation lest you think I'm even stranger than you probably already think I am :), but I finally decided on reporting the whole story so you have the full context of what I am trying to accomplish.

    You see, I'm writing a batch file that accepts its input from standard input (via "%@SafeExp[@Line[CON,0]]", to be exact), and if the input is from an actual file (rather than STDIN, which is unlikely, or a pipe, which is highly likely) I'd like the batch file to display a "percent done" figure when the right-shift key (just an arbitrary "choice") is depressed ala:
    Code:
    Iff %_RShift == 1 Then
       Iff "%ShiftStatus" == "" Then
          @Echo %ProgressIndication
          Set ShiftStatus=1
       EndIff
    Else
       Set ShiftStatus=
    EndIff   
    
    I'll note that the reason I want to do this is because this particular .btm "program" can take 10 to 15 minutes to run, and I'd like to be able to easily "query" its "current progress" (as a percent-done figure) whenever I want to.

    So, if standard input is from other than the console (the value of "%_STDIN" being zero which should virtually always be the case given what this batch file is intended to be used for) I would like to be able to find the name of the input file (if the input is coming from a file rather than a pipe) so that I can then use the "@Lines" function to return the number of lines in the file and then calculate and display the percent done. (I'd also like to do the same thing if the input is coming from a pipe, but I find that unlikely.) So, when I execute the "history" command from the command line, I get "PercentCommand <AnInputTestFile.txt randomparm asampleparm etc" as the second to last line of the "history" list (the last line being "history | list", of course) which is exactly what I would want because I could then "parse out" the input file name. However, if I execute the "history" command from within the batch file, the command line that invoked the batch file is displayed except for the fact all indications of the input redirection are gone; i.e., "Echo %@ExecStr[-1,History]" produces just "PercentCommand randomparm asampleparm", all indications of the input redirection being gone. And the same thing is true if I use "@ExecArray". (I must admit that I do find the fact that the history command returns somewhat different results when run from an "@Exec..." function in a batch file than when run directly from the command line to be a little bit strange, but that's not really important.) And I will add here that the "%CmdLine" "variable" returns exactly the same results as the the "@Exec...[History]" functions, which I really didn't find all that surprising but was the reason I was trying to use the "History" command in an "@Exec..." function in the first place.

    So the question is ultimately pretty simple: Is there a way I can accomplish what I am trying to accomplish and, if so, exactly how do I do that?

    - Dan
     
  2. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    Every batchfile and alias can use the value of the pseudovariable %# (number of parameters passed) to make decisions, INCLUDING performing completely unrelated actions. In a batch file you can use the command "SWITCH %#" and perform actions in "CASE 0", CASE 1", etc. sections. In an alias you need to us "IFF %# EQ 0 %+ then ..." constructs.
    --
    HTH, Steve
     
  3. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    Steve, I'm confused. I really don't understand your answer at all (even to the point of saying what "aspect" of your answer I don't understand at all). Whatever does that answer have to do with getting a "percent done" figure displayed when a particular key is pressed while the batch file is running and has the focus?

    - Dan

    P. S. BTW, I figured out a way to get exactly what I wanted, but it is quite slow in that it probably doubles the length of time it takes to run the batch file as a whole so I would still like to get an answer (if there is one) to my original question. And this "solution" was to open a temporary output file and copy standard input to that file and then close the file. Then the batch file opens that temporary file for input and does its thing except for now it has the total number of lines read in from standard input available (by counting each line in standard input as it is read - there isn't even really any need to use the @Lines function). And this solution even works when the input comes from a "pipe", and I kind of doubt if there is any other solution that would have that characteristic. And the reason this was an issue in the first place is because the input is 87,559 lines long.
     
  4. epement

    Joined:
    Jun 28, 2008
    Messages:
    67
    Likes Received:
    2
    I write filters like this all the time, 4DOS/TCC batch files that take either piped input from STDIN or a filename.

    Code:
    :: usage is a label for a brief helpmessage.
    if "%1" == "/h" .or. "%1" == "/?" .or. "%1" == "--help"  goto usage
    if "%1" == "" .and. %_pipe == 0 goto no_args
    if "%1" == "" goto filter
    if not exist %1 goto no_file
    :: else, we have at least 1 valid filename here ...
    :: do your work with expected filename here
    . . .
    quit
     
    :filter
    :: do your work with expected stdin here
    . . .
    quit
     
    :no_args
    beep  850 8  2 3  850 8  2 3  1250 11
    echoERR ERROR: No file to look for, nor any values from STDIN!
    echoERR Quitting here...
    quit 1
     
    :no_file
    beep  850 8  2 3  850 8  2 3  1250 11
    echoERR ERROR: Input file on command line, [%1], does not exist!
    echoERR Quitting here...
    quit 1
     
    :usage
    TEXT
    my usage message goes here
    ENDTEXT
    
    I notice that you use the system variable %_stdin, whereas I use %_pipe. Feel free to change it above, or let me know if I've totally missed the point. HTH.

    Eric P.
     
  5. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    Eric, somehow I wasn't very clear in my first posting, and I added some stuff to my second posting but stupidly didn’t notice that I hadn’t hit the "Post Reply" button so that update to the second posting had not happened by the time you read it. So here's the deal. I have a batch file that reads data from STDIN (which will always be from either a pipe (usually) or from a file (once in a while) and never, ever, under any conceivable circumstances, from the console. Because of the rather large amount of data that this batch file is expected to process (as I attempted to say in the posting that I hadn’t hit the “Post Reply” button for), 87,559 lines in this (the first) case; and the number of lines in the input data will always (again in any conceivable circumstances) be about that order of magnitude). While the processing performed on each line of input is relatively trivial, it still took roughly two hours or so (actually kind of a guess because I essentially forgot about it while it was running and did a bunch of other totally unrelated things, some of which weren't even on the computer) just to process the input data because there is so much of it. And, as I indicated in my first posting, I wanted to be able to hit a key (I rather arbitrarily chose the right shift key) and have the batch file echo a percent done figure (based on what line is being processed relative to the total number of lines in the input stream) to the console so I could at least make an estimate of how long the whole process is going to take (just gathering the input data takes several hours). I will note that the time to process each line of the input data is relatively constant, so if it takes 45 minutes to process about half the input data, it will take roughly an hour and a half to process all of the input data. And I will note again that the input data will almost always be from a pipe, so that a “solution” that only works when the input is a file, while better than nothing, is not even close to being optimal. And, as I indicated in that now completed second posting, I now have what is a “complete” solution in that it will handle input data from whatever source, file, pipe, or even STDIN (no processing actually starts until end-of-file is reached for the input data, no matter what its source), and since I really doubt that there is a technique that will allow me to directly know how many lines of input data are contained in a “pipe”, I tend to believe that this solution is about the best that can be achieved. (I’ve run it on “real” input data, and it wasn’t nearly as bad time-wise as I thought it would be.) And, as I indicate in my previous posting (which you of course hadn’t read when you made your posting), the “solution” was and is to copy the input data from STDIN to a temporary file (using @FileWrite), and then read the data from the temporary file (using @FileRead, of course), do the processing, and then echo it to STDOUT so that the next “program” in the “chain” (if any) can do its thing.

    And if anybody is curious about what I am actually trying to accomplish, it really isn't all that mysterious and is directly related to the fact that my C: drive is 98.8% full and my D: drive is 99.9842 % (!!!!) full.

    But, as always, thank you, guys!!! :)

    - Dan
     
  6. David Marcus

    Joined:
    Jun 4, 2008
    Messages:
    646
    Likes Received:
    1
    If I do "echo foo < bar.txt a b", I get "foo a b" because TCC is interpreting "< bar.txt" to mean that it should redirect stdin to read from bar.txt. If you replace the "Echo %@ExecStr[-1,History]" with "history > history.txt", do you get the full command line in the file?
     
  7. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    Thank you, David, you hit the nail on the head as the saying goes. If I replace "@Echo %@ExecStr[0,History]" by "@Echo %@SafeExp[@ExecStr[0,History]]" and the "@Echo %_ExecArray History[%@Dec[%_ExecArray]]" by "@Echo %_ExecArray %@SafeExp[History[%@Dec[%_ExecArray]]]", everything works as I would have hoped in that what I then get is:

    Code:
    [Z:\]PercentCommand <AnInputTestFile.txt randomparm asampleparm
    %CmdLine: PercentCommand  randomparm asampleparm
    PercentCommand <AnInputTestFile.txt randomparm asampleparm
    PercentCommand  randomparm asampleparm
    1  PercentCommand <AnInputTestFile.txt randomparm asampleparm
    
    I will note that doing "@Echo %%CmdLine: %@SafeExp[CmdLine]" does not work, but that's not particularly surprising to me since I tend to doubt that the redirection is technically part of the command line as "seen by" "%CmdLine".

    Somewhat surprisingly to me, replacing the "@Echo"'s by "@SafeEcho"'s doesn't work, and I'm really not quite sure why (although I have somewhat of a guess). But that's a purely academic question at this point and I really don't care all that much since the problem is solved.

    Thank you again for "clearing up" the mystery, David!

    - Dan
     

Share This Page