Jump to content

Search in file


6105
 Share

Recommended Posts

Hello,

Hi i will can to search in files? but not to use ReadFile but ReadFileLine something like from 100 to 200 needed word.

Thank you.

Tudor.

Edited by toader

[center][font=courier new,courier,monospace]Die die die my darling[/font][/center][center][font=courier new,courier,monospace]Don't utter a single word[/font][/center][center][font=courier new,courier,monospace]Die die die my darling[/font][/center][center][font=courier new,courier,monospace]Just shut your pretty mouth[/font][/center][center][font=courier new,courier,monospace]I'll be seeing you again[/font][/center][center][font=courier new,courier,monospace]I'll be seeing you[/font][/center][center][font=courier new,courier,monospace]In hell[/font][/center]

Link to comment
Share on other sites

Local $sFilePath = @ScriptDir & '\file.txt'
Local $sSearch = 'examplesearch'
Local $iLineNumber = 1
Local $iStopOnLine = 0
Local $iStartPos = 1, $sFound, $iPosб, $sLineText

While 1
    $sLineText = FileReadLine($sFilePath, $iLineNumber)
    If @error Then ExitLoop
    While 1
        $iPos = StringInStr($sLineText, $sSearch, 1, 1, $iStartPos)
        If Not $iPos Then ExitLoop
        If $iStartPos = 1 Then $sFound &= @CRLF & 'Positions in Line ' & $iLineNumber & ': '
        $sFound &= $iPos & ', '
        $iStartPos = $iPos+1
    WEnd
    If $iStopOnLine = $iLineNumber Then ExitLoop
    If $iStartPos > 1 Then
        $iStartPos = 1
        $sFound = StringTrimRight($sFound, 2)
    EndIf
    $iLineNumber += 1
WEnd

$sFound = StringTrimLeft($sFound, 2)

MsgBox(0, '', $sFound)

Edited by dwerf
Link to comment
Share on other sites

Thank you dwerf very mutch that work!!

but i has some troubles(

If _Search2 don't find nothing with $search2a i put it to do _Search3 with $search2b and in _Search3 that don't read the $search2b i chatch the bug, bug but can't fix it((( can u see? post-50212-12863782369385_thumb.png

#include <String.au3>
#include <Array.au3>
#include <File.au3>
$map = "c:\product2\"
$file = "\index.html"
Local $sSearch1 = 'productID'
Local $sSearch2a = '<a target="_blank"'
Local $sSearch2b = "img-current_picture"
Local $iLineNumber = 1
Local $iLineNumber2 = 1
Local $iStopOnLine = 0
Local $iStartPos = 1, $string, $sFound1, $iPos, $sLineText, $sFound2
Local $List=_FileListToArray($map, "*", 2)
Local $Array[$List[0] + 1][10]
    $s_start1='value="'
    $s_start2='tachments/SC/products_pictures'
    $s_end1='" type'
    $s_end2="' img_width="
    $s_end3='" title="'


For $i = 1 To $List[0]
    $Array[$i][0] = $List[$i]
    $sFilePath = $map & $List[$i] & $file
    _Search1()
    _Search2()
    $Line1 = FileReadLine($sFilePath, $sFound1)
    $snip1 = _StringBetween($Line1, $s_start1, $s_end1, -1)
        If @Error = 1 then
            Local $snip1[1]
            _ArrayInsert($snip1, 0, "N/A1")
        EndIf
    $Line2 = FileReadLine($sFilePath, $sFound2)
    $snip2 = _StringBetween($Line2, $s_start2, $s_end2, -1)
        If @Error = 1 Then
            MsgBox(0,'','first error')
            $snip2 = _StringBetween($Line2, $s_start2, $s_end3, -1)
            If @Error = 1 Then
                MsgBox(0,'','second error')
                Local $snip2[1]
                _ArrayInsert($snip2, 0, $Line2)
            EndIf
        EndIf

        $Array[$i][1] = $snip1[0]
        $Array[$i][2] = $snip2[0]
Next

Func _Search1()
    $iLineNumber = 1
    $iStartPos = 1
    $sFound1 = ''
    $iPos = ''
    $sLineText = ''
            While 1
            $sLineText = FileReadLine($sFilePath, $iLineNumber)
            If @error Then ExitLoop

            While 1
                $iPos = StringInStr($sLineText, $sSearch1, 1, 1, $iStartPos)
                If Not $iPos Then ExitLoop
                If $iStartPos = 1 Then
                    $sFound1 = $iLineNumber
                    ExitLoop
                EndIf
                $iStartPos = $iPos+1
            WEnd

            $iLineNumber += 1

        WEnd
EndFunc

Func _Search2()
    $iLineNumber = 1
    $iStartPos = 1
    $sFound2 = ''
    $iPos = ''
    $sLineText = ''
        While 1
            $sLineText = FileReadLine($sFilePath, $iLineNumber)
            If @error Then ExitLoop

            While 1
                $iPos = StringInStr($sLineText, $sSearch2a, 1, 1, $iStartPos)
                If Not $iPos Then ExitLoop
                If $iStartPos = 1 Then
                    $sFound2 = $iLineNumber
                    ExitLoop
                EndIf
                $iStartPos = $iPos+1
            WEnd

            $iLineNumber += 1

        WEnd
        If $sFound2 = '' then _Search3()
    EndFunc

Func _Search3()
    $iLineNumber = 1
    $iStartPos = 1
    $iPos = ''
    $sLineText = ''
        While 1
            ;===============//DEBUG//=====================
            
            $sLineText = FileReadLine($sFilePath, $iLineNumber)
            MsgBox(0,'',$sFilePath & ' | ' & $iLineNumber & ' | ' & $sSearch2b & '  |   ' & $sFound2 & @CRLF & $sLineText)
            If @error Then ExitLoop
            
            ;===============//DEBUG//=====================
            While 1
                $iPos = StringInStr($sLineText, $sSearch2b, 1, 1, $iStartPos)
                If Not $iPos Then ExitLoop
                If $iStartPos = 1 Then
                    $sFound2 = $iLineNumber
                    ExitLoop
                EndIf
                $iStartPos = $iPos+1
            WEnd

            If $iLineNumber > 1000 then ExitLoop

                $iLineNumber += 1

        WEnd
    EndFunc

Func _write1()
        $content1 = ""
        $file1 = "C:\product2\log\test1.txt"
        FileClose(FileOpen($file1, 2))       ;create the file

        For $i=1 To UBound($Array)-1
            $content1 &= $Array[$i][0]&@CRLF
        Next
        FileWrite($file1, $content1)
    EndFunc

Func _write2()
        $content2 = ""
        $file2 = "C:\product2\log\test2.txt"
        FileClose(FileOpen($file2, 2))       ;create the file

        For $i=1 To UBound($Array)-1
            $content2 &= $Array[$i][1]&@CRLF
        Next
        FileWrite($file2, $content2)
    EndFunc

Func _write3()
        $content3 = ""
        $file3 = "C:\product2\log\test3.txt"
        FileClose(FileOpen($file3, 2))       ;create the file

        For $i=1 To UBound($Array)-1
            $content3 &= $Array[$i][2]&@CRLF
        Next
        FileWrite($file3, $content3)
    EndFunc

;   _write1()
;   _write2()
;   _write3()

    _ArrayDisplay($Array)

Thank you so mutch!

Edited by toader

[center][font=courier new,courier,monospace]Die die die my darling[/font][/center][center][font=courier new,courier,monospace]Don't utter a single word[/font][/center][center][font=courier new,courier,monospace]Die die die my darling[/font][/center][center][font=courier new,courier,monospace]Just shut your pretty mouth[/font][/center][center][font=courier new,courier,monospace]I'll be seeing you again[/font][/center][center][font=courier new,courier,monospace]I'll be seeing you[/font][/center][center][font=courier new,courier,monospace]In hell[/font][/center]

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