Jump to content

How to determine the Line number of a text file


deef99
 Share

Recommended Posts

I am writing a script that will:

1. Open a text file

2. Search for the occurance of "=400"

Got that working.

Now I need to know what LINE that is so I can grab the information on the line AFTER that.

Can someone give me an idea how I can do that?

I tried: $position = StringInStr($read, "400") But this gives me the character position.

Appreciate any help!

Dee

Link to comment
Share on other sites

I am writing a script that will:

1. Open a text file

2. Search for the occurance of "=400"

Got that working.

Now I need to know what LINE that is so I can grab the information on the line AFTER that.

Can someone give me an idea how I can do that?

I tried: $position = StringInStr($read, "400") But this gives me the character position.

Appreciate any help!

Dee

There are several ways to accomplish this. Please post the code that you have so far. This will help us to know the direction that you were taking and maybe we can continue in that same direction.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Read it to an array with _FileReadToArray(), get the index for "=400" with _ArraySearch(), then just increment the index +1.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Read it to an array with _FileReadToArray(), get the index for "=400" with _ArraySearch(), then just increment the index +1.

:)

This is all I have so far...

I don't know how to get _FileReadtoArray to work...can u help please?

#include "array.au3"

#include "File.au3"

#include "Date.au3"

#include "String.au3"

$position = 0

$sl = 0

$

; Change DIR and process files just moved

FileChangeDir("\\xxxfiles01\common\CMS_IBAuto\Monitor")

;look for first occurance of a file

$search = FileFindFirstFile("*.txt")

MsgBox(0, "1", $search)

While 1

;look for additional files

$file = FileFindNextFile($search)

MsgBox(0, "2", $file)

If @error Then ExitLoop

$file1 = FileOpen($file, 0)

MsgBox(0, "3", $file1)

$read = FileRead($file1)

If @error = -1 Then

MsgBox(0, "Error", "File not read")

Exit

Else

MsgBox(0, "Read", $read)

If StringRegExp($read, "= 400") Then

$position = StringInStr($read, "= 400")

MsgBox(0, "Oops", $position)

Else

MsgBox(0, "Oops", "No match")

EndIf

EndIf

FileClose($file)

Wend

Edited by deef99
Link to comment
Share on other sites

HI,

it would be easier if you describe what your goal is. If you need the line after a specific search word then this could be easily done by one regex pattern.

Please, describe a little more what you want and we can tell you how to do it.

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

Link to comment
Share on other sites

This is all I have so far...

I don't know how to get _FileReadtoArray to work...can u help please?

You have to declare the variable beforehand and pass it ByRef to _FileReadToArray(). That's the only thing even slightly tricky about it, unless you mean you don't know how to use arrays yet.

#include "array.au3"
#include "File.au3"
#include "Date.au3"
#include "String.au3"
$position = 0
$sl = 0
$
; Change DIR and process files just moved
FileChangeDir("\\xxxfiles01\common\CMS_IBAuto\Monitor")
;look for first occurance of a file
$search = FileFindFirstFile("*.txt")
MsgBox(0, "1", $search)
While 1
    ;look for additional files
    $file = FileFindNextFile($search)
    MsgBox(0, "2", $file)
    If @error Then ExitLoop

$file1 = FileOpen($file, 0)
MsgBox(0, "3", $file1)
$read = FileRead($file1)
If @error = -1 Then
    MsgBox(0, "Error", "File not read")
    Exit
Else
    MsgBox(0, "Read", $read)
    If StringRegExp($read, "= 400") Then
        $position = StringInStr($read, "= 400")
        MsgBox(0, "Oops", $position)
    Else
        MsgBox(0, "Oops", "No match")
    EndIf
EndIf
FileClose($file)
Wend
Try it this way for simplicity's sake:
Global $sFile, $sLine
Global $sDir = "\\xxxfiles01\common\CMS_IBAuto\Monitor"
Global $hSearch = FileFindFirstFile($sDir & "\*.txt")

While 1
    ;look for additional files
    $sFile = FileFindNextFile($hSearch)
    If @error Then 
        ExitLoop
    Else
        $sFile = $sDir & "\" & $sFile   
        ConsoleWrite("Debug:  $sFile = " & $sFile & @LF)
    EndIf
    
    $sLine = FileReadLine($sFile)
    If @error Then
        ConsoleWrite("Debug:  Exit on FileReadLine(), @error = " & @error & @LF)
        Exit
    Else
        If StringInStr($sLine, "=400") Then
            ; Read next line
            $sLine = FileReadLine($sFile)
            ConsoleWrite("Debug:  $sLine = " & $sLine & @LF)
            Exit
        EndIf
    EndIf
WEnd

:)

Edit: Xenobiologist is right, FileRead() and StringRegExp() could do the search in two lines, but you didn't sound like you were ready to play with RegExp.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

GUYS: thanks alot...your insight helped me figure out what I needed!!!!!

I have another question, but I will post as new thread...THANKS AGAIN!

You have to declare the variable beforehand and pass it ByRef to _FileReadToArray(). That's the only thing even slightly tricky about it, unless you mean you don't know how to use arrays yet.

Try it this way for simplicity's sake:

Global $sFile, $sLine
Global $sDir = "\\xxxfiles01\common\CMS_IBAuto\Monitor"
Global $hSearch = FileFindFirstFile($sDir & "\*.txt")

While 1
    ;look for additional files
    $sFile = FileFindNextFile($hSearch)
    If @error Then 
        ExitLoop
    Else
        $sFile = $sDir & "\" & $sFile   
        ConsoleWrite("Debug:  $sFile = " & $sFile & @LF)
    EndIf
    
    $sLine = FileReadLine($sFile)
    If @error Then
        ConsoleWrite("Debug:  Exit on FileReadLine(), @error = " & @error & @LF)
        Exit
    Else
        If StringInStr($sLine, "=400") Then
            ; Read next line
            $sLine = FileReadLine($sFile)
            ConsoleWrite("Debug:  $sLine = " & $sLine & @LF)
            Exit
        EndIf
    EndIf
WEnd

:)

Edit: Xenobiologist is right, FileRead() and StringRegExp() could do the search in two lines, but you didn't sound like you were ready to play with RegExp.

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