Jump to content

Searching inside .txt files.


Thidian
 Share

Recommended Posts

Hello AutoIt users, me again (Sigh)

Had this problem for a while now and done alot of thinking and testing.

What I need is to:

1. Copy a file to .txt format (Config file used in a game)

2. Scan through the .txt to find the line(s) the program needs.

3. Stop when the above step is completed.

4. Send the results to the user via some sort of GUI (Already fixed)

Here's the code I'm working with:

#include <file.au3>
Global $3
$path =FileOpenDialog("Point me to your Configuration.WTF", @HomeDrive, "WTF (*.WTF)")
$path2 = @ScriptDir & "\configuration.txt"
FileCopy($path, @ScriptDir & "\configuration.txt", 0)
$x = 1
$1 = FileOpen($path2, 0)
$2 = FileRead($1)
$c = _FileCountLines($path2)
msgbox(0, "", $c)
for $x = 1 to $c
    Do
$3 = FileReadLine($1, $x)
$12 = stringsplit($3, '"')
MsgBox(0, $x, $12[1])
if  $12[1] = "The value I need" then
MsgBox(0, "", ""); Or as in my full script -> guictrlsetdata($lab1, $12[1])
Exit
EndIf
Sleep(10)

$x =$x+1
until $x = $c
Next

Any help or point to a tutorial/already solved thread is apreciated as the search function isn't really giving me the results I need.

~Thidian.

Edited by Thidian
Link to comment
Share on other sites

You're actually overdoing it.

A Do/Until inside a For/Next will make your script execute the code between $c*$c times ... ouch..

Here is a solution (no need to copy the file).

#include <file.au3>
Global $file_content, $split
$path =FileOpenDialog("Point me to your Configuration.WTF", @HomeDrive, "WTF (*.WTF)")
_FileReadToArray($path, $file_content)
For $i = 1 To $file_content[0]
    $split = StringSplit($file_content[$i], '"')
    If $split[1] = "The value I need" Then
        MsgBox(0, "result", $split[1])
        ExitLoop
    EndIf
Next

Try to understand my example.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Yet again you help me out in the easiest way possible.

Thanks once again! <3

Will try this out and I'm not sure wether AutoIt opens the .WTF extension as it should be (Could be mistaken, had several issues 2 years ago) but I'll try and post back with results.

~Thidian

Okey, posting back with the results.

By using the code you provided, and a small msgbox to display the current value of $split[1], it seems to skip the value I need when it gets to it.

For example, I need a line called "SET MaxFPS", which is the first result that comes up when opening.

The messagebox tells me that it's currently reading "SET MaxFPS", it still goes through the whole file until the end and then exits.

Edited by Thidian
Link to comment
Share on other sites

To employ an "exact match" ( If $split[1] = "The value I need" Then ) might not be the best idea unless you KNOW it will look exactly like that. If it doesn't (it has an extra white space, missing a white space, another extra character ...) the code won't work.

The best way is to use StringInStr and StringStripWS

Let me show you an example:

If $split[1] = "The value I need" Then

- match for The value I need

- no match The value I need (this one was supposed to have an extra space between "value" and "I")

- no match The value I need1

In order to have a better match, you should use: If StringInStr(StringStripWS($split[1], 8), "SETMaxFPS") Then

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

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