Jump to content

Search values from array inside a txt file


Recommended Posts

Hy,

I have an array with some values. I must search one by one the values into the text file and return the line.

I have this but...

 

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

Global $aRetArray, $sFilePath
Global $file = "D:\files.txt", $search
_FileReadToArray("C:\032017.txt", $aRetArray)

_ArrayDelete($aRetArray, 0)

Global $iRows = UBound($aRetArray, $UBOUND_ROWS)

_ArrayDisplay($aRetArray)


Global $iLine = 0, $sLine = ''
Global $hFile = FileOpen($file)
If $hFile = -1 Then
    MsgBox(0,'ERROR','Unable to open file for reading.')
    Exit 1
EndIf

; find the line that has the search string
While 1
    $iLine += 1
    $sLine = FileReadLine($hFile)
    If @error = -1 Then ExitLoop

    ; $search found in the line, now write the next 5 lines to STDOUT
   For $j = 0 To $iRows - 1
      $search = $aRetArray[$j]
      If StringInStr($sLine, $search) Then
         For $i = $iLine To $iLine
            ;ConsoleWrite(FileReadLine($hFile, $i) & @CRLF)
            FileCopy(FileReadLine($hFile, $i), "Z:\JD\", 0)
         Next
         ExitLoop
      Else
            ConsoleWrite($search & @CRLF)
      EndIf
   Next
WEnd
FileClose($hFile)

 

Learn, learn and ... learn

Link to comment
Share on other sites

its working fine in my side and I removed Exitloop to be able to write in console "C:\Test\Test7"

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

Global $aRetArray, $sFilePath
Global $file = "C:\Test\Test7.txt", $search
_FileReadToArray("C:\Test\Test6.txt", $aRetArray)

;~ _ArrayDelete($aRetArray, 0)

_ArrayDisplay($aRetArray, "First")


Global $iLine = 0, $sLine = ''
Global $hFile = FileOpen($file)
If $hFile = -1 Then
    MsgBox(0, 'ERROR', 'Unable to open file for reading.')
    Exit 1
EndIf

; find the line that has the search string
While 1
    $iLine += 1
    $sLine = FileReadLine($hFile)
    If @error = -1 Then ExitLoop

    ; $search found in the line, now write the next 5 lines to STDOUT
    For $j = 0 To UBound($aRetArray) - 1
        $search = $aRetArray[$j]
        If StringInStr($sLine, $search) Then
            For $i = $iLine To $iLine
                MsgBox(0, "", "$i=" & $i & @CRLF & "Line= " & $iLine & @CRLF & "Search= " & $search)
                ConsoleWrite(FileReadLine($hFile, $i) & @CRLF)
                FileCopy(FileReadLine($hFile, $i), "C:\Test\New folder\", 0)
            Next
        Else
            ConsoleWrite("SEarch Else: " & $search & @CRLF)
        EndIf
    Next
WEnd
FileClose($hFile)

 

Edited by 232showtime
you can do above post as well.

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Link to comment
Share on other sites

reallyshyfox,

Some questions:

1 - How big are your search arg and searched files?

2 - What if there is more than one occurrence of the search arg in the searched file?

3 - What format is the searched/search arg file?

4 - What, exactly, is your expected output?

Thanks,

kylomas

Edited by kylomas
spelling

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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