Jump to content

Find specific word in TXT or Find after ":" or Read specific row and column.


Go to solution Solved by Nine,

Recommended Posts

Posted

Hello everyone,
Sorry for any mistakes in English, as I'm using google translator.

Please I would like some help.

The source code is in the attachment, because I don't know how to insert it here in the text. For correct operation, you must remove the comment from Requires Administrator

I'm making a script that works like inventory, including getting CDP and LDP data.

But when looking for a monitor's serial number in a TXT file (in the temp folder), I can get it normally.

What I need is:

How to fetch specific part of TXT? Example: I would like to throw in a variable only the result that I had written in the Monitor Name: and Serial Number: lines.

I tried 3 ways and failed:

Primary idea: If AutoIT is unable to read specific words within the TXT (which would be ideal).

or

The TXT will always save the Monitor Name on line 15 - for example, but I don't want the whole line, but starting from the character ":"

or

Or line 15 from column 28.

Could you please help me?

TotalInventory.au3

Posted (edited)

Here one way :

Local $aList, $sText, $sMon, $sSerial

$sText = FileRead("Data_Monitor.txt")
$aList = StringRegExp($sText, "(?mi)^Monitor Name\h*:\h*(.*)$", 1)
If IsArray($aList) Then $sMon = $aList[0]
$aList = StringRegExp($sText, "(?mi)^Serial Number\h*:\h*(.*)$", 1)
If IsArray($aList) Then $sSerial = $aList[0]

ConsoleWrite("Monitor Name = " & $sMon & @CRLF & "Serial Number = " & $sSerial & @CRLF)

ps. in your txt file, there is 2 monitors.  Is this 2 different examples, or do you want to get both at the same time ? My example code only gets the first, but it can be easily adapted to get both (or multiple).

Edited by Nine
Posted (edited)

Here something that shows multiple monitors :

#include <Array.au3>
#include <Constants.au3>

Local $sText, $aMon, $aSerial

$sText = FileRead("Data_Monitor.txt")
$aMon = StringRegExp($sText, "(?mi)^Monitor Name\h*:\h*(.*)$", 3)
If IsArray($aMon) Then _ArrayDisplay($aMon)
Local $aSerial = StringRegExp($sText, "(?mi)^Serial Number\h*:\h*(.*)$", 3)
If IsArray($aSerial) Then _ArrayDisplay($aSerial)

If Not IsArray($aMon) Or Not IsArray($aSerial) Or UBound($aMon) <> UBound($aSerial) Then _
  Exit MsgBox($MB_SYSTEMMODAL, "Error", "Missing informations")

ConsoleWrite("You have " & UBound($aMon) & " monitors :" & @CRLF)
For $i = 0 To UBound($aMon) - 1
  ConsoleWrite("Monitor " & $i+1 & @CRLF & "Monitor Name = " & $aMon[$i] & @CRLF & "Serial Number = " & $aSerial[$i] & @CRLF & @CRLF)
Next

 

Edited by Nine
added support of no found information
Posted (edited)

@Nine

Yes, it was very good, the result is exactly what I expected.
I'll read it slowly to learn how you did the coding.

I'm just having a problem. As I'm in learning, I'm trying to remove ArrayDisplay, to get the results and throw in a GuiCreateLabel and I couldn't.

For each monitor and serial number a GuiCreateLabel.

Example:

Monitor 01: Dell Serial: CKMASD2234
Monitor 02: HP Serial: OHASDFSD@

The result of the serial being a GuiCreateLabel to later be saved. (In Red)

ImageApp.JPG

Edited by ARPFre
Add pic
Posted

I looked at your code.  You seem to recreate label at the same spot multiple times.  You should not do that.  Instead just assign your GUIcreateLabel to a variable.  And then after, just use GUICtrlSetData to modify (or clear) the value of the label.

Concerning you request, what do you want to achieve exactly ?  Do you want all name/SN be displayed in a single label, or do you want to have a label for each monitor ?

Try to put a small snippet of the code that would replace the ArrayDisplay, so you can learn along with it...

Posted

@Nine

Thanks for the "GUIcreateLabel and GUICtrlSetData" tip, I don't know these handlers, I'll look at Help. I believe it's easy.

I want a label for each monitor. That is my objective.
Without showing the (Display Array).

Posted

@Nine 

Will you help me one more time?

In your code I still couldn't remove the Array window that appears, every time I modify the error code and the application does not open.

  • Solution
Posted

Alright, I will show you how you should post code correctly, see link.  When you are asked to provide a snippet you can do something like this :

#include <Constants.au3>

....

; Label creation for up to 3 monitors - adapt screen localization and size
Local $aMonLabel[3]
For $i = 0 to UBound($aMonLabel) - 1
  $aMonLabel[$i] = GUICtrlCreateLabel("", 500, 200 + $i * 30, 100, 25)
Next

....

; Read the name and serial from the txt file
Local $sText, $aMon, $aSerial
$sText = FileRead("Data_Monitor.txt")
$aMon = StringRegExp($sText, "(?mi)^Monitor Name\h*:\h*(.*)$", 3)
Local $aSerial = StringRegExp($sText, "(?mi)^Serial Number\h*:\h*(.*)$", 3)
If Not IsArray($aMon) Or Not IsArray($aSerial) Or UBound($aMon) <> UBound($aSerial) Then _
  Exit MsgBox($MB_SYSTEMMODAL, "Error", "Missing informations")

; assign retrieved values to labels
For $i = 0 To UBound($aMon) - 1
  GUICtrlSetData($aMonLabel[$i], "Monitor " & $i+1 & " : " & $aMon[$i] & " - Serial : " & $aSerial[$i])
Next

untested of course...

Posted

@Nine I can't even thank you for the teachings.
I'm learning a lot from your code.

It is solving my doubts and serving as a learning experience.

Thank you so much!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...