Jump to content

Read text file and find certain data


Recommended Posts

I can't for the life of me figure this portion of my script out.

How can I read a file and then find something like "PCI\VEN" and then the rest of the string?

This should work:

#include <File.au3>

$PATH = FileOpenDialog("SELECT",@ScriptDir,"All (*.*)",1)
If @error Then Exit

$FILE = FileOpen($PATH,0)
For $INDEX = 1 To _FileCountLines($PATH)
    $LINE = FileReadLine($FILE,$INDEX)
    $FIND = StringInStr($LINE,"PCI\VEN")
    If Not @error And $FIND <> 0 Then MsgBox(0,"FOUND",$LINE)
Next
FileClose($FILE)

When the words fail... music speaks.

Link to comment
Share on other sites

This should work:

#include <File.au3>

$PATH = FileOpenDialog("SELECT",@ScriptDir,"All (*.*)",1)
If @error Then Exit

$FILE = FileOpen($PATH,0)
For $INDEX = 1 To _FileCountLines($PATH)
    $LINE = FileReadLine($FILE,$INDEX)
    $FIND = StringInStr($LINE,"PCI\VEN")
    If Not @error And $FIND <> 0 Then MsgBox(0,"FOUND",$LINE)
Next
FileClose($FILE)
Thanks Andreik! I'll give that a shot!

I'm basically trying to mimic this post in AutoIT

http://forum.driverpacks.net/viewtopic.php?pid=16120#p16120

Edited by kickarse
Link to comment
Share on other sites

Ok, so once I get the string I'm trying to extract only a certain section of the string. I tried to use _StringBetween but I can't seem to get the PCI\VEN to end of the line to work. Anyone??

Please explain a little more. What section of the string?

When the words fail... music speaks.

Link to comment
Share on other sites

Below is the code I currently have... I'm trying from the line below everything including and after PCI\VEN or up to a semi colon...

%DEVICE_DESCRIPTION_9K% = Install, PCI\VEN_13C1&DEV_1002&SUBSYS_100213C1 would be PCI\VEN_13C1&DEV_1002&SUBSYS_100213C1

%aec6280.DeviceDesc% = aec6280_Inst, PCI\VEN_1191&DEV_0009;&SUBSYS_62801191 would be PCI\VEN_1191&DEV_0009

I'm also wanting to get rid of all lines that are comments so anything like

;%aec6280.DeviceDesc% = aec6280_Inst, PCI\VEN_1191&DEV_0009;&SUBSYS_62801191

wouldn't even be listed...

#Include <Array.au3>
#Include <File.au3>
#include <String.au3>

dim $array2,$avarray3,$avFileToArray
$Root = StringLeft(@WindowsDir,3)

_FindDrivers("C:\D\M")

exit


Func _FindDrivers($driverLocation)
    $Array = RecursiveFileSearchwithoutFileNames($driverLocation, "(?i)\.(inf|INF)", ".", 1)
; Get Unique Elements
    $avResult = _ArrayElements($Array, 1)
    ToolTip("Building List of HWIDS",0,0,"Gathering HWIDS")
    FOR $element IN $avResult
        $FILE = FileOpen($element,0)
        For $INDEX = 1 To _FileCountLines($element)
            $LINE = FileReadLine($FILE,$INDEX)
            $FIND = StringInStr($LINE,"PCI\VEN_",0,-1)
    ;FileWriteLine($root &"\hwids.extracted.txt",$LINE)
            If Not @error And $FIND <> 0 Then MsgBox(0,"FOUND",$LINE)
        Next
        $INDEX = ""
        FileClose($FILE)
    NEXT

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.2.10.0
    Author: WeaponX
    Updated: 2/21/08
    Script Function: Recursive file search
    
    2/21/08 - Added pattern for folder matching, flag for return type
    1/24/08 - Recursion is now optional
    
    edited by Kickarse
    7/25/2008 - Script Function changed to scan for all inf/INF files and only display their folders
    
    
    Parameters:
    
    RFSstartdir: Path to starting folder
    
    RFSFilepattern: RegEx pattern to match
    "\.(mp3)" - Find all mp3 files - case sensitive (by default)
    "(?i)\.(mp3)" - Find all mp3 files - case insensitive
    "(?-i)\.(mp3|txt)" - Find all mp3 and txt files - case sensitive
    
    RFSFolderpattern:
    "(Music|Movies)" - Only match folders named Music or Movies - case sensitive (by default)
    "(?i)(Music|Movies)" - Only match folders named Music or Movies - case insensitive
    "(?!(Music|Movies)\b)\b.+" - Match folders NOT named Music or Movies - case sensitive (by default)
    
    RFSFlag: Specifies what is returned in the array
    0 - Files and folders
    1 - Files only
    2 - Folders only
    
    RFSrecurse: TRUE = Recursive, FALSE = Non-recursive
    
    RFSdepth: Internal use only
    
#ce ----------------------------------------------------------------------------

Func RecursiveFileSearchwithoutFileNames($RFSstartDir, $RFSFilepattern = ".", $RFSFolderpattern = ".", $RFSFlag = 0, $RFSrecurse = True, $RFSdepth = 0)

;Ensure starting folder has a trailing slash
    If StringRight($RFSstartDir, 1) <> "\" Then $RFSstartDir &= "\"

    If $RFSdepth = 0 Then
;Get count of all files in subfolders for initial array definition
        $RFSfilecount = DirGetSize($RFSstartDir, 1)

;File count + folder count (will be resized when the function returns)
        Global $RFSarray[$RFSfilecount[1] + $RFSfilecount[2] + 1]
    EndIf

    $RFSsearch = FileFindFirstFile($RFSstartDir & "*.*")
    If @error Then Return

;Search through all files and folders in directory
    While 1
        $RFSnext = FileFindNextFile($RFSsearch)
        If @error Then ExitLoop

;If folder and recurse flag is set and regex matches
        If StringInStr(FileGetAttrib($RFSstartDir & $RFSnext), "D") Then

            If $RFSrecurse And StringRegExp($RFSnext, $RFSFolderpattern, 0) Then
                RecursiveFileSearchwithoutFileNames($RFSstartDir & $RFSnext, $RFSFilepattern, $RFSFolderpattern, $RFSFlag, $RFSrecurse, $RFSdepth + 1)
                If $RFSFlag <> 1 Then
            ;Append folder name to array
                    $RFSarray[$RFSarray[0] + 1] = $RFSstartDir & $RFSnext
                    $RFSarray[0] += 1
                EndIf
            EndIf
        ElseIf StringRegExp($RFSnext, $RFSFilepattern, 0) And $RFSFlag <> 2 Then
    ;Append file name to array
    ;$RFSarray[$RFSarray[0] + 1] = StringTrimRight($RFSstartDir, 1);& $RFSnext
            $RFSarray[$RFSarray[0] + 1] = $RFSstartDir & $RFSnext
            $RFSarray[0] += 1
        EndIf
    WEnd
    FileClose($RFSsearch)

    If $RFSdepth = 0 Then
        ReDim $RFSarray[$RFSarray[0] + 1]
        Return $RFSarray
    EndIf
EndFunc ;==>RecursiveFileSearchwithoutFileNames


;===============================================================================
; FunctionName:  _ArrayElements()
; Description:    Returns the number of unique elements from a 1D or 2D array
; Syntax:          _ArrayElements( $aArray, $iStart )
; Parameter(s):  $aArray - ByRef array to return unique elements from (array is not changed)
;                  $iStart - (Optional) Index to start at, default is 0
; Return Value(s):  On success returns an array of unique elements,  $aReturn[0] = count
;                  On failure returns 0 and sets @error (see code below)
; Author(s):        jon8763; Modified by PsaltyDS
;===============================================================================
Func _ArrayElements(ByRef $aArray, $iStart = 0)
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)

; Setup to use SOH as delimiter
    Local $SOH = Chr(01), $sData = $SOH

; Setup for number of dimensions
    Local $iBound1 = UBound($aArray) - 1, $Dim2 = False, $iBound2 = 0
    Select
        Case UBound($aArray, 0) = 2
            $Dim2 = True
            $iBound2 = UBound($aArray, 2) - 1
        Case UBound($aArray, 0) > 2
            Return SetError(2, 0, 0)
    EndSelect

; Get list of unique elements
    For $m = $iStart To $iBound1
        If $Dim2 Then
    ; 2D
            For $n = 0 To $iBound2
                If Not StringInStr($sData, $SOH & $aArray[$m][$n] & $SOH) Then $sData &= $aArray[$m][$n] & $SOH
            Next
        Else
    ; 1D
            If Not StringInStr($sData, $SOH & $aArray[$m] & $SOH) Then $sData &= $aArray[$m] & $SOH
        EndIf
    Next

; Strip start and end delimiters
    $sData = StringTrimRight(StringTrimLeft($sData, 1), 1)

; Return results after testing for null set
    Local $avRET = StringSplit($sData, $SOH)
    If $avRET[0] = 1 And $avRET[1] = "" Then Local $avRET[1] = [0]
    Return $avRET
EndFunc ;==>_ArrayElements

;=============== END - FUNCTIONS
Edited by kickarse
Link to comment
Share on other sites

I`m not sure is is exactly what you want:

#include <File.au3>

$PATH = FileOpenDialog("SELECT",@ScriptDir,"All (*.*)",1)
If @error Then Exit

$FILE = FileOpen($PATH,0)
$FOUND_LINES = FileOpen(@ScriptDir & "\Lines.txt",2)
For $INDEX = 1 To _FileCountLines($PATH)
    $LINE = FileReadLine($FILE,$INDEX)
    $FIND1 = StringInStr($LINE,"PCI\VEN")
    $FIND2 = StringInStr($LINE,",")
    If Not @error And $FIND1 <> 0 And $FIND2 <> 0 Then FileWriteLine($FOUND_LINES,$LINE)
Next
FileClose($FOUND_LINES)
FileClose($FILE)

When the words fail... music speaks.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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