Jump to content

Could use some help with _StringInStr and _StingBetween


Recommended Posts

I'm trying to search a file for a given string. Once I have that string, I want to get a users name out of it. Then search again for a new string including that users name. Once I have the second string, I want to get their reply out of it.

The section of code below looks right to me but it doesn't seem to find what I'm searching for nor does it return the _StringBetween values. I know the file has the info I'm looking for.

The file is of unknown and varying length but should have only one instance of each thing I am searching for.

I appreciate any help on this. Thanks

$File = FileOpen ("C:\testfile.txt",0)
$LineCount = _FileCountLines($File)
For $Line = 1 To $LineCount
    If StringInStr(FileReadLine($File, $Line), "Attention ") Then
        $RqstTo = _StringBetween(FileReadLine($File, $Line),"Attention ",", please forward the information.") 
    EndIf
Next
For $line = 1 To $LineCount
    If StringInStr(FileReadLine($File, $line), $RdstTo & " replies, ") Then
        $Rspns = _StringBetween(FileReadLine($File, $line),$RqstTo & "'s reply, ",".")
    EndIf
Next

The TestFile looks like:

This is a test file
This is line two of the file
Attention Mike, please forward the information.
This is line four of the file
Mike's reply, Right away.

The results I am looking for are:

$RqstTo = "Mike" (from line 3)

$Rspns = "Right away" (from line 5)

absolution (Tault)

Edited by crazyjts
Link to comment
Share on other sites

I just ran a test and for some reason the $LineCount value = 0 even though there are 5 lines in the TestFile.txt file.

I think this may be the issue and not my If Then loops.

Anyone see what I'm doing wrong with this?

Edited by crazyjts
Link to comment
Share on other sites

_StringBetween returns an array. Instead of reading each line separately, you can use fileread. Try this.

#include <Array.au3>
#include <String.au3>
$File = FileRead("C:\testfile.txt")
$Line3 = _StringBetween($File, "Attention ", ", please")
$Line5 = _StringBetween($File, "Mike's reply, ", ".")
_ArrayDisplay($Line3)
_ArrayDisplay($Line5)
Link to comment
Share on other sites

_StringBetween returns an array. Instead of reading each line separately, you can use fileread. Try this.

#include <Array.au3>
#include <String.au3>
$File = FileRead("C:\testfile.txt")
$Line3 = _StringBetween($File, "Attention ", ", please")
$Line5 = _StringBetween($File, "Mike's reply, ", ".")
_ArrayDisplay($Line3)
_ArrayDisplay($Line5)
I'm not up to snuff on arrays so maybe _StringBetween isn't what I should use.

Is there an "easy" way to find a string within a sting assuming I know the beginning and end of the string?

Let's say my string is "Attention Mike, please forward the information."

Can I somehow search for the string containing "Attention " and then find the string that sits between "Attention " and ", please forward the information." and then use that found string in another search?

Link to comment
Share on other sites

_StringBetween returns an array. Instead of reading each line separately, you can use fileread. Try this.

#include <Array.au3>
#include <String.au3>
$File = FileRead("C:\testfile.txt")
$Line3 = _StringBetween($File, "Attention ", ", please")
$Line5 = _StringBetween($File, "Mike's reply, ", ".")
_ArrayDisplay($Line3)
_ArrayDisplay($Line5)
I spent some time tonight and figured this out. Thank you Dantay.
Link to comment
Share on other sites

I spent some time tonight and figured this out. Thank you Dantay.

Ok so I didn't get it as well as I thought I did. I've got 2 problems:

1) I cannot get the $Rqst = _StringBetween($File, $Who & "tells you, '","'.") to work

2) I cannot figure out how to use $Who & $Rqst in a message box.

The array display shows the values as I expect but when I write up a basic msgbox none of the text shows up. I guess it comes down to that fact that I don't understand how to use the array values in another operation.

Any help is appreciated.

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

$File = FileRead("C:\log.txt")
$Who = _StringBetween($File, "You ask ",", 'What would you like?'")
$Rqst = _StringBetween($File, $Who & " tells you, '","'.")
_ArrayDisplay($Who, "Name")
_ArrayDisplay($Rqst, "Request")
MsgBox(0,"Info",$Who & " wants " & $Rqst)

Log.txt file

Logging ON
Current log file size: 0 KB
You ask Elvis, 'What would you like?'.
Elvis tells you, 'Fried banana and peanut butter sandwich'.
Link to comment
Share on other sites

If you use _StringBetween it returns an array, so if it finds a match you'll have to specify an element to display. You should use:

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

$File = FileRead("C:\log.txt")
$Who = _StringBetween($File, "You ask ",", 'What would you like?'")
$Rqst = _StringBetween($File, $Who[0] & " tells you, '","'.")
_ArrayDisplay($Who, "Name")
_ArrayDisplay($Rqst, "Request")
MsgBox(0,"Info",$Who[0] & " wants " & $Rqst[0])oÝ÷ ØZ+­Ûzk2¢æ«yÚ"¢}ý¶ØbKajܨºwu©Ú®¶²¶Þ¶êç²)ÜzØ^­íý²åÈãD«'ç¢ÛayƬz('¢·¢YhÂ)àbjw[aÇîËb¢y®r²'«jwb¶·­º¹ìjËkx'¢Ö§jºÚÉ(§qè­j^j»-«r¢æ«zZ()à~ÿ«Þ¢yrÖf­rh"w%¹×¬ç²Æ«É«­¢+ØÀÌØí¥±ô¥±I ÅÕ½ÐíèÀäÈí±½¹ÑáÐÅÕ½Ðì¤((ÀÌØí]¡¼ô}MÑÉ¥¹ Ñݹà ÀÌØí¥±°ÅÕ½Ðíe½ÔͬÅÕ½Ðì°ÅÕ½Ðì°Ìäí]¡Ðݽձå½Ô±¥­üÌäìÅÕ½Ðì¤)%ÀÌØí]¡¼Q¡¸ÀÌØíIÅÍÐô}MÑÉ¥¹    Ñݹà ÀÌØí¥±°ÀÌØí]¡¼µÀìÅÕ½Ðìѱ±Ìå½Ô°ÌäìÅÕ½Ðì°ÅÕ½ÐìÌäì¸ÅÕ½Ðì¤)5Í    ½à À°ÅÕ½Ðí%¹¼ÅÕ½Ðì°ÀÌØí]¡¼µÀìÅÕ½ÐìݹÑÌÅÕ½ÐìµÀìÀÌØíIÅÍФ()Õ¹}MÑÉ¥¹  Ñݹà ÀÌØíMÑÉ¥¹°ÀÌØíMÑÉаÀÌØí¹¤(IÑÕɸMÑÉ¥¹IáÁIÁ± ÀÌØíMÑÉ¥¹°ÅÕ½Ðì ý¤¥mléÍ¥¤éut¨ÀäÈíDÅÕ½ÐìµÀìÀÌØíMÑÉеÀìÅÕ½ÐìÀäÈí ¸¨¤ÀäÈíDÅÕ½ÐìµÀìÀÌØí¹µÀìÅÕ½ÐìÀäÈímléÍ¥¤éut¨ÅÕ½Ðì°ÅÕ½ÐìÀÌØìÄÅÕ½Ðì¤)¹Õ¹ìôôÐí}MÑÉ¥¹    Ñݹà
Edited by Varian
Link to comment
Share on other sites

I tried something very similar to this thinking this was the right approach. I get the same error with your code and with mine.

C:\Users\Name\Desktop\Array Test.au3 (16) : ==> Subscript used with non-Array variable.:

$Rqst = _StringBetween($File, $Who[0] & " tells you, '","'.")

$Rqst = _StringBetween($File, $Who^ ERROR

It seems not to recognize the [0] position which seems to me to be the only position in the array since it is only looking for one thing.

Link to comment
Share on other sites

Ugh...one more problem I didn't foresee.

I have an instance where the string "You ask Elvis 'What would you like?'" does not exist in the file. I believe the _StringBetween array for $Who is coming back empty and causing an error. How do I check if the line exists or not prior to using the $Who = _StringBetween($File, "You ask ",", 'What would you like?'") function?

Edited by crazyjts
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...