1.

Solve : Need help using the type command ouput as variable?

Answer»

I'm using a product called temperature alert.  It creates .log files with temperature dumps.  If I run a "TYPE" command on today's log, I receive several LINE entries.

Example of the last one, which is the one I want to capture:

11/7/2011 11:39:55 AM,68.34



I want to set a variable to 68.34

Please Help!
You can use the tokenising ability of the FOR command to capture a desired PART of a line.

Am I right that you want to:

1. Get the last line of a log file

 and

2. Isolate the final part after the (only) comma?

If so you could do it like this

Code: [Select]for /f "tokens=1-2 delims=," %%A in ( ' type "logfile.txt" ' ) do set variable=%%B
echo Final temperature in log is %variable%

explanation:

One-line FOR /F syntax:

for /f "token block" %%variable in (dataset) do action

Multiline FOR /F syntax:

for /f "token block" %%variable in (dataset) do (
        action1
        action2
        action3
        etc
        )


for /f ... the /f switch means "treat the dataset as a series of lines to be processed"

"token block" ... set various parameters governing splitting the line up into a series of one or more tokens, separated by stated delimiters

"tokens=1-2 delims=," ... split each line into 2 tokens, the first token being everything up to the first comma, the second token being from the first comma to the next comma or the end of the line, whichever comes first. Since there is
only one comma, this effectively isolates the temperature reading in the second comma.

%%variable ... Variable is a single letter a-z or A-Z. If the line is being split into tokens then the first token is assigned to the explicit (declared) variable (%%A in the example) and subsequent tokens are assigned to the implicit variables starting with the next letter of the alphabet (%%B in this case) So if you want 26 tokens (tha maximum for one FOR line) you'd better make the first one %%A or %%a (case matters, %%a is not equivalent to %%A)

dataset ... What we want FOR to process, since we used the /F switch this is the source of the lines to process, in this case the output of the type command. To use a command or program's CONSOLE output in this way we place a single quote before and after the command.  The spaces in the example above are for clarity and are not compulsory.

action ... what you want to do with the token or line (in this case assign it to a variable)

The FOR /F command loops through the output of the command line by line, performing the action on each in turn. When the loop ends, the variable will HOLD the last value assigned.

for full documentation of the FOR command type for /? at the prompt








Thanks!  That is exactly what i asked for. 

Anyway to avoid the overhead of processing the whole file and just get it to skip to the last line?  When I launch, it runs through setting the variable 400 times before getting the final result.  Thanks! Quote from: choward16980 on November 11, 2011, 11:05:11 PM

Thanks!  That is exactly what i asked for. 

Anyway to avoid the overhead of processing the whole file and just get it to skip to the last line?  When I launch, it runs through setting the variable 400 times before getting the final result.  Thanks!

Surely if you have echo turned off (e.g. if you have echo off at the start of the batch) then the time taken to do this will be very short indeed - much less than one seond on any kind of modern computer less than about 20 years old. Otherwise you could install a Windows port of the Unix TAIL utility, I guess.



Discussion

No Comment Found