Jump to content

Getting data from a table in TXT.


Go to solution Solved by Subz,

Recommended Posts

Posted

Good morning everyone, Sorry for errors in English I'm using Google Translate.

I apologize for the code, as I have no idea how to start, I already tried a few lines but nothing worked.

I have a table in TXT with results of :

wmic product get Name, Version, InstallDate

How to get specific software showing results as below (Example)

Kaspersky Endpoint Security for Windows - Version 11.2.0.2254 - Installed on  2020/23/11

Getting data from a table in TXT.

 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 437, 192, 124)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
 

InstallSoftware.txt

Posted (edited)

@Nine 

 

I did this one, and it only returns the line number, how to bring the result of the version and installation date?
What is the next step? How should I code some hint?

 

$Texto = FileRead("\THI\InstallSoftware.txt")

Local $iPosition =   StringInStr($Texto, "Cisco AnyConnect Secure Mobility Client")

MsgBox($MB_SYSTEMMODAL, "", "The search string 'Cisco AnyConnect Secure Mobility Client' first appears at position: " & $iPosition)

Edited by ARPFre
Posted (edited)

You could try splitting the file into an array and then searching to get the results, for example:
 

#include <Array.au3>
Local $sFilePath = @ScriptDir & "\InstallSoftware.txt"
Local $sFileData = FileRead($sFilePath)
Local $sHeader = FileReadLine($sFilePath, 1)
Local $iColumn1 = StringInStr($sHeader, "Name") - 1
Local $iColumn2 = StringInStr($sHeader, "Version") - $iColumn1 - 1
Local $iColumn3 = StringLen($sHeader) - $iColumn1 - $iColumn2 - 1
Local $aFileData1D = StringRegExp($sFileData, "(.{" & $iColumn1 & "})(.{" & $iColumn2 & "})(.{" & $iColumn3 & "})", 3)
Local $aFileData2D = _Array1DTo2D($aFileData1D, 3)
Local $iSearch = _ArraySearch($aFileData2D, "Cisco AnyConnect Secure Mobility Client", 0, 0, 0, 1, 1, 1)
MsgBox(4096, "Result", "Name : " & $aFileData2D[$iSearch][1] & @CRLF & "Version : " & $aFileData2D[$iSearch][2])
_ArrayDisplay($aFileData2D)

; #FUNCTION# ====================================================================================================================
; Name ..........: _Array1DTo2D
; Description ...: Transforms a 1D to a 2D array.
; Syntax ........: _Array1DTo2D($avArray, $iCols[, $iStart = 0[, $iEnd = 0[, $iFlag = 0]]])
; Parameters ....: $avArray             - Array to modify.
;                  $iCols               - Number of columns to transform the array to.
;                  $iStart              - [optional] Index of array to start the transformation. Default is the first element.
;                  $iEnd                - [optional] Index of array to stop the transformation. Default is the last element.
;                  $iFlag               - [optional] If set to 1, the array size must to a multiple of $iCols. Default is 0.
; Return values .: Success : Returns a 2D array
;                  Failure : Returns 0 and sets @error to :
;                    1 - $aArray is not an array
;                    2 - $iStart is greater than $iEnd
;                    3 - $aArray is not a 1D array
;                    4 - $aArray size is not a multiple of $iCols
; Author ........: jguinch
; ===============================================================================================================================
Func _Array1DTo2D($avArray, $iCols, $iStart = 0, $iEnd = 0, $iFlag = 0)

    If $iStart = Default OR $iStart < 0 Then $iStart = 0
    If $iEnd = Default Then $iEnd = 0

    If NOT IsArray($avArray) Then Return SetError(1, 0, 0)
    If UBound($avArray, 0) <> 1 Then Return SetError(3, 0, 0)

    Local $iUBound = UBound($avArray) - 1

    If $iEnd < 1 Then $iEnd = $iUBound
    If $iEnd > $iUBound Then $iEnd = $iUBound
    If $iStart > $iEnd Then Return SetError(2, 0, 0)

    Local $iNbRows = ($iEnd - $iStart + 1) / $iCols
    If $iFlag AND IsFloat($iNbRows) Then Return SetError(2, 0, 0)

    Local $aRet[ Ceiling($iNbRows) ][$iCols]
    Local $iCol = 0, $iRow = 0
    For $i = $iStart To $iEnd
        If $iCol = $iCols Then
            $iCol = 0
            $iRow += 1
        EndIf
        $aRet[$iRow][$iCol] = $avArray[$i]
        $iCol += 1
    Next

    Return $aRet
EndFunc

 

Edited by Subz
Posted

@Subz -

I completely copied your script and pasted it on my PC, to see the result and learn. But it didn't work, it returned the following error:

$aFileData2: possibly used before declaration.
_ArrayDisplay($aFileData2)

 

 

test.au3

Posted

For fun, a case sensitive search example

#include <Array.au3>
$file = "InstallSoftware.txt"

_Read($file, 'Acrobat')
_Read($file, 'Silverlight')
_Read($file, 'Kaspersky')
_Read($file, 'AnyConnect')
_Read($file, 'Microsoft Visual C')

Func _Read($file, $sString)
    $a = StringSplit(StringRegExpReplace(StringRegExp(FileRead($file), "(?i)[\w ]+\Q" & $sString & "\E.*", 1)[0], '\s{3,}', "|"), "|", 2)
    $a[0] = StringRegExpReplace($a[0], "(\d{4})(\d{2})(\d{2})(.*)", "$1/$2/$3")
    _ArrayDisplay($a, $sString)
EndFunc

 

Posted

@Deye

Thanks also for the reply.

But I have a doubt:

I tried to add an exception handle and I couldn't.

When it doesn't find the _Read or when it doesn't find the Software, a Message Box could appear.

Posted

Of course, then you have to separate it out.

#include <Array.au3>
$file = "InstallSoftware.txt"

_ArrayDisplay(_Read($file, 'Acrobat'))
_ArrayDisplay(_Read($file, 'Silverlight'))

Func _Read($file, $sString)
    Local $sfile = FileRead($file)
    If Not $sfile Then Return ;And\Or do something
    Local $a = StringRegExp($sfile, "(?i)[\w ]+\Q" & $sString & "\E.*", 1)
    If Not IsArray($a) Then Return ;And\Or do something
    Local $ab = StringSplit(StringRegExpReplace($a[0], '\s{2,}', "|"), "|", 2)
    If StringLen($ab[0]) = 8 Then _dateFormat($ab)
    Return $ab
EndFunc

Func _dateFormat(ByRef $a)
    $a[0] = StringRegExpReplace($a[0], "(\d{4})(\d{2})(\d{2})", "$1/$2/$3")
EndFunc

 

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...