Author Topic: File.Read not able to read txt file output from command line  (Read 2289 times)

0 Members and 1 Guest are viewing this topic.

PegLegTV

  • $upporter
  • Hero Member
  • *****
  • Posts: 500
  • Karma: 43
    • View Profile
I have a command that checks my CPU level and lets me know if its running "high" for more then 30 minutes,

it stopped working a while back and I finally had time to dig into the cause, and it looks like File.Read isn't reading the command line output file ("CPULoad.txt") correctly it should be:

Quote
LoadPercentage 
46         
if I open the file and check it manually it's written corectly but when I use File.Read {LastResult} becomes
Quote
��L

If I write a file manually with the same text, File.Read sets {LastResult} Correctly so I think it has to do with the fact that its generated by the command line.

here's a command to show you what I'm talking about

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.2.2-->
<command id="691" name="CPU Checker (1)" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="This will create 2 text files called &quot;CPULoad.txt&quot; and &quot;CPULoad2.txt&quot; in your VC Folder">
  <action>
    <cmdType>Launch.CMD</cmdType>
    <params>
      <param>wmic cpu get loadpercentage&gt; CPULoad.txt</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>VC.Pause</cmdType>
    <params>
      <param>2000</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>File.Read</cmdType>
    <params>
      <param>{Path.VC}\CPULoad.txt</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>{LastResult}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

This command will create a text file called "CPULoad.txt" in your VC Folder
« Last Edit: August 02, 2016, 09:23:36 AM by jitterjames »

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2319
  • Karma: 47
    • View Profile
Re: File.Read not able to read txt file output from command line
« Reply #1 on: August 02, 2016, 04:54:34 AM »
Maybe it has to do with the formatted text which the command line generate.

Here is what I found for the questionmark symbol: http://www.unicode.org/charts/PDF/UFFF0.pdf

FFFD

REPLACEMENT CHARACTER

used to replace an incoming character whose
value is unknown or unrepresentable in
Unicode

compare the use of 001A

  as a control
character to indicate the substitute function

2BD1

  uncertainty sign
« Last Edit: August 02, 2016, 09:23:53 AM by jitterjames »
***********  get excited and make things  **********

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2012
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: File.Read not able to read txt file output from command line
« Reply #2 on: August 02, 2016, 08:54:54 AM »
Launch.Capture is generally well-suited for this kind of thing. e.g.

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.2.2-->
<command id="691" name="CPU Checker using Launch.Capture" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="Use Launch.Capture to capture to directly get commandline output.">
  <action>
    <cmdType>Launch.Capture</cmdType>
    <params>
      <param>c:\windows\system32\cmd.exe</param>
      <param>/C wmic cpu get loadpercentage</param>
      <param>True</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.RegEx</cmdType>
    <params>
      <param>(\d+)</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>{Match.1}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>Get CPU load</phrase>
</command>

« Last Edit: August 02, 2016, 09:24:08 AM by jitterjames »
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: File.Read not able to read txt file output from command line
« Reply #3 on: August 02, 2016, 09:26:51 AM »
On a side note you don't need to use {Path.VC} and 2 seconds is probably much longer than you need unless your computer is severely overloaded.

I will look into the File.Read issue. Not sure what encoding the cmd.exe uses, or why.
« Last Edit: August 05, 2016, 04:24:01 PM by nime5ter »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: File.Read not able to read txt file output from command line
« Reply #4 on: August 02, 2016, 09:31:50 AM »
See also: http://voxcommando.com/forum/index.php?topic=1836.msg15948#msg15948

If you are checking load on a regular interval it makes more sense to do it in a python thread so you are not interfering with the regular operation of VC.

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2012
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: File.Read not able to read txt file output from command line
« Reply #5 on: August 02, 2016, 10:53:42 AM »
Here is a threaded Python solution. Currently checks load% once a minute, and generates an event in VC if load% is > 10%.

Change values as desired.

Download attached cpuLoad.py file and place in \PY folder of VC.

VC command to load script on launch:
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.2.2-->
<command id="58" name="Load and run Python script on launch" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecFile</cmdType>
    <params>
      <param>PY\cpuLoad.py</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <event>VC.Loaded</event>
</command>

The python code in the attached file is:
Code: [Select]
import clr,time,thread
clr.AddReference("System.Management")
from System.Collections.Generic import *
from System.Management import ManagementObject

def getLoad(secs):

    while not killPy:
       mo = ManagementObject("Win32_PerfFormattedData_PerfOS_Processor.Name='_total'")
       load = mo["PercentProcessorTime"]
       #print load
       
       #load% over 10% triggers VC event. Change this val as desired. Passes load% val as event payload {1}.
       if load > 10:
          vc.triggerEvent("CPU.load", List[str]([str(load)]))
       
       #pause between loops:currently 60 secs
       time.sleep(secs)
       
thread.start_new_thread(getLoad, (60,))

It's a variation on a post from a while back: http://voxcommando.com/forum/index.php?topic=1836.msg15948#msg15948
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

PegLegTV

  • $upporter
  • Hero Member
  • *****
  • Posts: 500
  • Karma: 43
    • View Profile
Re: File.Read not able to read txt file output from command line
« Reply #6 on: August 02, 2016, 12:35:13 PM »
Thanks for all the Help and input,

On a side note you don't need to use {Path.VC} and 2 seconds is probably much longer than you need unless your computer is severely overloaded.

If I do a 1 second pause or less it would grab the old text, on all pc's that I have ran it on it took at least 1300 milliseconds so I just made it 2 seconds even to be sure (Tested on: Win 7, Win 8/8.1, and Win 10). for the {Path.VC} I noticed that Last night when I uploaded it, I just copied the Actions from the Full command that I use, (that shows how long ago I made it  :bonk)



See also: http://voxcommando.com/forum/index.php?topic=1836.msg15948#msg15948

If you are checking load on a regular interval it makes more sense to do it in a python thread so you are not interfering with the regular operation of VC.
  Right now the command that I made checks the CPU every 20 minutes, but if the CPU is above 40% then it will check it again in 10 minutes, and if it is still high, then it will notify me, its an amd CPU so the Percentage runs a little higher then normal usually around 15%.

Launch.Capture is generally well-suited for this kind of thing. e.g.
I just tested Launch.Capture and it works great, I will switch over to Launch.Capture for now, I'll have to check out the python method you guys have recommended as well

Maybe it has to do with the formatted text which the command line generate.

Here is what I found for the questionmark symbol: http://www.unicode.org/charts/PDF/UFFF0.pdf

FFFD

REPLACEMENT CHARACTER....


I'm going to be honest with you kalle, I fully understood the first two sentences, but after that I was lost, to technical for me  :bonk


I just dug up two older versions of VC and Tested 2.2.1.4 and File.Read worked perfectly on the command line output text file

VC 2.2.1.6 - File.Read gave me the same output as VC 2.2.2.2, so it must have changed in Version 2.2.1.5 or 2.2.1.6 (would have Tested 2.2.1.5 but I don't have an exe for that one) hope this can help narrow down the problem

Thanks again for all the help guys  :hugs