Sign in to follow this  
Followers 0
Waeolwen

Line number of a line in a text file?

10 posts in this topic

Basically like the title says... I'm reading through a text file line by line, and I want to be able to replace lines that meet specific criteria with blank lines (or delete them completely). Is there a way to retrieve the line number of the line I just got from ReadLine? :)

Share this post


Link to post
Share on other sites



No need, just have a counter increase everytime you read a line :)


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Dim $MrArray
_FileReadToArray("wootfile.dll", "MrArray")
For $i=1 To $MrArray[0]
MsgBox(0, "", $MrArray[$i])
Next

A Little example of a counter (Well not really...but whatever!)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Dim $MrArray
 _FileReadToArray("wootfile.dll", "MrArray")
 For $i=1 To $MrArray[0]
 MsgBox(0, "", $MrArray[$i])
 Next

A Little example of a counter (Well not really...but whatever!)

Now what was that? :)

A much better example would be this:

$handle=FileOpen("hello.txt",0)

$inc=0
While True
    $inc+=1
    $data=FileReadLine($handle)
    If $data="" Then ExitLoop
    MsgBox(0,"Line number: "&$inc,$data)
WEnd
Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Haha! Your missing a end parenthese... :)

Share this post


Link to post
Share on other sites

Haha! Your missing a end parenthese... :)

Ooops, I guess that's for writing the code directly at the website :)

Fixed now btw.


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Well those kind of work, my only problem is that the _FileWriteToLine command requires a line number to write to the line...

I can make it work perfectly on the first write, but each one after that is off by at least one line.

Share this post


Link to post
Share on other sites

Make a For/Next loop, and use:

_FileWriteToLine() and for the line use: $Line[$i] or whatever.

Share this post


Link to post
Share on other sites

Make a For/Next loop, and use:

_FileWriteToLine() and for the line use: $Line[$i] or whatever.

Got it working after some math thoughts :) My counter wasn't adding enough...

$final = FileOpen("C:\Program Files\Everquest\Logs\Apr_02.txt", 0)
    $fPath = $ePath & "\" & $monthS & "_" & $daystored & ".txt"
    $intQ = 0
    While 1
        $lineZ = FileReadLine($final)
        If @error = -1 Then ExitLoop
        $intQ = $intQ + 1
        Select
            Case StringInStr($lineZ, "~~~> STARTING LOG") <> 0
                While 1
                    $lineQ = FileReadLine($final)
                    $intQ = $intQ + 1
                    If StringInStr($lineQ, "<Triune>") = 0 Then
                        _FileWriteToLine($fPath, $intQ, " ", 1)
                    ElseIf StringInStr($lineQ, "<Triune>") <> 0 Then
                        ExitLoop
                    EndIf
                    $intQ = $intQ + 1
                WEnd
            Case StringInStr($lineZ, "~~~> STARTING LOOT") <> 0
                While 1
                    $lineQ = FileReadLine($final)
                    $intQ = $intQ + 1
                    If StringInStr($lineQ, "grat") = 0 Then
                        _FileWriteToLine($fPath, $intQ, " ", 1)
                    ElseIf StringInStr($lineQ, "grat") <> 0 Then
                        ExitLoop
                    EndIf
                WEnd
        EndSelect
    WEnd
    ShellExecute($fPath)

Share this post


Link to post
Share on other sites

Hi,

here is an old udf I made for myself :

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

Global $path = "c:\tree.txt"
Global $NewPath = "c:\test1.txt"
Global $search = "au3"

;$h_path, $s_search, $i_opt = 1, $i_caseSensitive = 0, $i_count = 0, $i_topDown = 0, $h_pathNew = "", $i_inEx = 0
$start = TimerInit()
_removeLineInFile($path, $search, 4, 0, 0, 0, "", 1)
MsgBox(0,"", TimerDiff($start))
#cs
; keep last 2 lines starting  with "Autoit" not case sensitive searching bottomUp
_removeLineInFile($path, $search, 2, 0, 5, 1, "", 1)
; keep all lines containing "Autoit"
_removeLineInFile($path, $search, 4, 0, 0, 0, "", 1)
; deletes all lines = "Autoit" case sensitive
_removeLineInFile($path, $search, 1, 1)
; deletes the first 2 lines = "Autoit"  not case sensitive searching bottomUp
_removeLineInFile($path, $search, 1, 0, 2, 1)
; deletes all lines containing the string "Autoit"
_removeLineInFile($path, $search, 4)
; deletes the first line containing the string "Autoit" searching bottomUp
_removeLineInFile($path, $search, 4, 0, 1, 1)
; deletes the first 5 lines starting with "Autoit" case sensitive, searching topDown and save the result in $newPath
_removeLineInFile($path, $search, 4, 1, 5, 0, $NewPath)
; deletes all lines ending with "Autoit" case sensitive, searching topDown and save the result in $newPath
_removeLineInFile($path, $search, 3, 1, 0, 0, $NewPath)

MsgBox(64, "To see the return value", _removeLineInFile($path, $search, 3, 0, 0, 1)) ; display the return value
#ce
;===============================================================================
;
; Function Name:   _removeLineInFile
; Description::       _removeLineInFile
; Parameter(s):
;                   1. $h_path = Path to file
;                   2. $s_search = string pattern
;                   3. Opt = 1 Delete/keep line if matches $s_search
;                      Opt = 2 Delete/keep line if starts with $s_search
;                      Opt = 3 Delete/keep line if ends with $s_search
;                      Opt = 4 Delete/keep line if contains $s_search
;                   4. 0 = not case sensitive (default)
;                      1 = case sensitive
;                   5. $i_count = How many occurrances should be deleted
;                   6. $i_TopDown = 0 (default) search --> 1-end
;                      $i_TopDown = 1 search bottumUp --> end-1
;                   7. $h_pathNew = "" file will be overwritten
;                      $h_pathNew = ... the result is saved in new path
;                   8. $i_inEx = 0 delete
;                      $i_inEx = 1 keep
;
; Requirement(s):  #include <File.au3> and #include <Array.au3>
; Return Value(s):  1 success
;                    -1 file not found
;                    -2 invalid $opt
;                     -3 invalid path to write
;                     Extented = Number of lines matched
; Author(s):       Xenobiologist
;
;===============================================================================
;

Func _removeLineInFile($h_path, $s_search, $i_opt = 1, $i_caseSensitive = 0, $i_count = 0, $i_topDown = 0, $h_pathNew = "", $i_inEx = 0)
    Local $i_countFound = 0
    Local $a_FileOne
    Local $a_FileTwo[1]
    Local $a_FileThree[1]
    
    If $h_pathNew = "" Then $h_pathNew = $h_path
    If $i_count = 0 Then $i_count = -1
    
    If Not _FileReadToArray($h_path, $a_FileOne) Then Return -1
    
    _ArrayDelete($a_FileOne, 0) ; do not need the number
    
    If $i_topDown = 1 Then _ArrayReverse($a_FileOne)
    
    For $i = 0 To UBound($a_FileOne) - 1
        If $i_countFound <> $i_count Then
            Switch $i_opt
                Case 1
                    If $i_caseSensitive = 1 Then
                        If $a_FileOne[$i] == $s_search Then
                            $i_countFound += 1
                            _ArrayAdd($a_FileThree, $a_FileOne[$i])
                        Else
                            _ArrayAdd($a_FileTwo, $a_FileOne[$i])
                        EndIf
                    Else
                        If $a_FileOne[$i] = $s_search Then
                            $i_countFound += 1
                            _ArrayAdd($a_FileThree, $a_FileOne[$i])
                        Else
                            _ArrayAdd($a_FileTwo, $a_FileOne[$i])
                        EndIf
                    EndIf
                Case 2
                    If $i_caseSensitive = 1 Then
                        If StringLeft($a_FileOne[$i], StringLen($s_search)) == $s_search Then
                            $i_countFound += 1
                            _ArrayAdd($a_FileThree, $a_FileOne[$i])
                        Else
                            _ArrayAdd($a_FileTwo, $a_FileOne[$i])
                        EndIf
                    Else
                        If StringLeft($a_FileOne[$i], StringLen($s_search)) = $s_search Then
                            $i_countFound += 1
                            _ArrayAdd($a_FileThree, $a_FileOne[$i])
                        Else
                            _ArrayAdd($a_FileTwo, $a_FileOne[$i])
                        EndIf
                    EndIf
                Case 3
                    If $i_caseSensitive = 1 Then
                        If StringRight($a_FileOne[$i], StringLen($s_search)) == $s_search Then
                            $i_countFound += 1
                            _ArrayAdd($a_FileThree, $a_FileOne[$i])
                        Else
                            _ArrayAdd($a_FileTwo, $a_FileOne[$i])
                        EndIf
                    Else
                        If StringRight($a_FileOne[$i], StringLen($s_search)) = $s_search Then
                            $i_countFound += 1
                            _ArrayAdd($a_FileThree, $a_FileOne[$i])
                        Else
                            _ArrayAdd($a_FileTwo, $a_FileOne[$i])
                        EndIf
                    EndIf
                Case 4
                    If StringInStr($a_FileOne[$i], $s_search, $i_caseSensitive) = 0 Then
                        _ArrayAdd($a_FileTwo, $a_FileOne[$i])
                    Else
                        $i_countFound += 1
                        _ArrayAdd($a_FileThree, $a_FileOne[$i])
                    EndIf
                Case Else
                    Return -2
            EndSwitch
        Else
            If $i_inEx = 0 Then
                _ArrayAdd($a_FileTwo, $a_FileOne[$i])
                _ArrayAdd($a_FileThree, $a_FileOne[$i])
            EndIf
        EndIf
    Next
    _ArrayDelete($a_FileTwo, 0)
    _ArrayDelete($a_FileThree, 0)
    
    If $i_topDown = 1 Then
        _ArrayReverse($a_FileTwo)
        _ArrayReverse($a_FileThree)
    EndIf
    
    If $i_inEx = 0 Then
        _FileWriteFromArray($h_pathNew, $a_FileTwo)
    Else
        _FileWriteFromArray($h_pathNew, $a_FileThree)
    EndIf
    SetExtended($i_countFound)
    If @error = 0 Then Return 1
    Return -3
EndFunc   ;==>_removeLineInFile

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
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
Sign in to follow this  
Followers 0