Jump to content

Searching a text file backwards.


Recommended Posts

Hi,

I tried to search for this, but while I did get a lot of results, nothing with what I wanted.

I need a script that searches a text file line by line backwords.

Basically I need to be able to find the first line from bottom that starts with "Jump".

Once I have that line identified, I just want to copy it to the clipboard.

The stupid thing is, I made a script a few years ago that did that. But then I lost it and now I cant remember what commands I used. I dont use Autoit very often and at best I can just read the help file and try to make basic scripts using that. But in this case, I just cant find what I need. Hopefully someone here can help.

Link to comment
Share on other sites

I'd go with

Fileread

_stringreverse

stringregexp for the entire line containing "pmuj"

*_stringreverse

clipput

*edit - you'd need to reverse back i suppose

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I just remmebered the command I used.

This is what I tried.

#include <File.au3>
$CountLines = _FileCountLines("1.txt")
$file = FileOpen("1.txt", 0)
While 1
$line = FileReadLine ( "1.txt" , $CountLines)
If NOT StringInStr ($line, "Jump to " , 1, 1, 1, 8) = 0 Then ExitLoop
$CountLines = $CountLines -1
Wend
Clipput ($line)
Exit

Seems to work fine with a test sample. Lets hope it works as good where its meant to.

Thanks anyway boththose. Appriciate the help. :D

Edited by Storm
Link to comment
Share on other sites

ahh you just wanted the line number. I believe the help file would recommend using the handle rather than the file name in your filereadline.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

That script actually gives me the full line. Not just the number.

Now I have added other stuff to it so its all finished.

The only thing Im stuck on is how to use a hotkey that functions as a normal.

Like if I want to set Alt+0 as the hotkey, when pressed, I want Alt+0 to go as normal and do what it would usually do as well as activating the hotkey in the script. Currently it just seems to block the use of ALt+0 and just activates the hotkey.

Edited by Storm
Link to comment
Share on other sites

yup seems fine, dont know why my paste resulted in a 1 the previous run.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

it isn't the best solution but using _IsPressed() is close to what you want.

there are 2 problems though:

1. you have to loop it as an if statement. you cant execute it from anywhere like you can with HotKeySet()

2. it will execute multiple times if the button is held unless you have a Sleep(), which could effect usability.

heres an example:

#include <Misc.au3>

Local $hDLL = DllOpen("user32.dll")
$ctr = 0

While $ctr <> 50
     If _IsPressed(12, $hDLL) Then
          If _IsPressed(30, $hDLL) Or _IsPressed(60, $hDLL) Then ;two here so it can be the num pad 0 or the other 0
               _insertFunctionHere()
               Sleep(100)
          EndIf
     EndIf
WEnd

Func _insertFunctionHere()
     Send('1')
     $ctr += 1
EndFunc

when i ran it and held down Alt+0 i got this output: 110110111001111010101101100110110101011110100110110101101101011101110011010

so it calls the function and still registers the keys.

[spoiler]My UDFs: Login UDF[/spoiler]

Link to comment
Share on other sites

_IsPressed only works if the user is actually holding the key down, right?

In my case it will just be a normal press. Also, since the script will end up checking for the press every 100 or so miliseconds, will that slow down windows? The people who will be using this will most likely be on laptops or netbooks. Is it safe ot use or can it cause system sluggishness by taking up resources?

Right now Im getting around it by disabling the hotkey, then running Alt+0, then setting the hotkey to Alt+0 again. And the rest of the script follows after that. It seems to work fine, but 1 out of 5 times it doesent do a thing, so I have to press it again.

Still, I will give it a try with this method and see which one feels better to use.

Its just strange that Autohotkey has an option for this but Autoit doesent.

Link to comment
Share on other sites

_IsPressed() will return true if the key is pressed, held, etc. and since it's only checking every 100 milliseconds or so it wont slow down windows at all. i usually put the sleep at 10 which is more than enough time, but here i didn't want the function called too many times.

[spoiler]My UDFs: Login UDF[/spoiler]

Link to comment
Share on other sites

  • Moderators

Storm,

Using FileReadLine as you have in post #3 above is an appalling coding technique. Each time you call that function with a line number you force the script to read right through the file each time until that line number - imagine if you have several thousand lines to read each time! :o

The best way to do it is to use _FileReadToArray and then search the array in memory. Here are some representative timings from my tests on a 2000 line file where I was searching for text in the first line: ;)

FileReadLine: 4292.44422126276
FileReadToArray: 17.6231895394526

Quite a difference - I hope you agree! :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Yea, you are right. Though I might leave it like as it is simply because this file is actually part of the game log. It gets trimmed every time it goes over 100 lines. And 95% of the times, the line it is looking for is one of the bottom 5.

Still thanks for the heads up. Im not very good with arrays, so I will read up on them to see how they work. Once I understand them, I will fix up the script.

Until then, this is what the final script looks like. This is the full script and not just a code. Im sure it can be optimized by using more efficient scripts, but for now, this is getting the job done. I scarpped the hotkey entirely, and just waited for the script to check for the log file creation. As soon as someone uses the command in game to save the log, it will create the file, which will in turn activate the script.

#include <File.au3>
$scriptname = @ScriptName
$ini = StringReplace ( $scriptname, "exe", "ini" , -3, 0)
        If FileExists($ini) = 1 Then 
                $var = FileReadLine(@ScriptDir & "" & $ini,2)   
                $var = $var & "Chat"
                $chat = FileReadLine(@ScriptDir & "" & $ini,5)
                $chat = "Chat_" & $chat & ".txt"
        Else 
                $var = FileSelectFolder("Select your RebirthRO game folder.", "")
                If @error = 1 Then Exit
                $answer = MsgBox(4, "RebirthRO Chat Tab name", "Have you renamed the chat tab that shows you @jump information to something other than General?")
                If $answer = 7 Then
                        $tabname = "General"
                Else
                        $tabname = InputBox("Chat Tab Name", "What is the chat tab name?")
                EndIf
                If @Error = 1 Then
                        Exit
                EndIf
                FileOpen(@ScriptDir & "" & $ini, 1)
                FileWrite (@ScriptDir & "" & $ini, "[Game Folder Location]" & @CRLF )
                FileWrite (@ScriptDir & "" & $ini, $var & @CRLF )
                FileWrite (@ScriptDir & "" & $ini, ""& @CRLF )
                FileWrite (@ScriptDir & "" & $ini, "[Chat Tab name]" & @CRLF )
                FileWrite (@ScriptDir & "" & $ini, $tabname )
                $var = FileReadLine(@ScriptDir & "" & $ini,2)   
                $var = $var & "Chat"
                $chat = FileReadLine(@ScriptDir & "" & $ini,5)
                $chat = "Chat_" & $chat & ".txt"

        EndIf 
FileChangeDir ( $var )
Pause()


Func Main()
$CountLines = _FileCountLines($chat)
While 1 
        $line = FileReadLine ( $chat , $CountLines)
        If NOT StringInStr ($line, "Jump to "  , 1, 1, 1, 8) = 0 Then ExitLoop
        $CountLines = $CountLines -1
        If $CountLines = 1 Then Pause()
Wend
While 1 
        $CountLines = $CountLines -1
        $line = FileReadLine ( $chat , $CountLines)
        If NOT StringInStr ($line, "Jump to "  , 1, 1, 1, 8) = 0 Then ExitLoop
        If $CountLines = 1 Then Pause()
Wend

$jump = StringReplace ( $line, "Jump to", "@jump" , 1, 0)
Clipput ($jump)
Pause()
EndFunc


Func Pause()
        FileDelete ("Chat_*")
        While 1
                Sleep(80)
                If FileExists($chat) = 1 Then Main()
        WEnd
EndFunc

The one thing I would have liked to do was use the FileDelete command to delete only txt files starting with "Chat_".

I tried a few things, and I could either use *.txt for all text files or Chat_* for all files starting with Chat_.

But I couldnt use Chat_*.txt. Its not really a big deal, but as it stands there is like a one in a million chance that the user might browse for the wrong folder and accidently delete a file.

I suppose that would come under user erorr, and I really doubt its ever gonna happen, but I would still like to know for future, on if its possible to use a wildcard like that.

Link to comment
Share on other sites

Steve... the next time you report a thread asking me to make sure it's okay, please don't do it when the post contains "code" this bad. It makes me want to cry to see code in such a state. The last language I used was Python which produces simple, elegant and beautiful code. Now you show me this massacre and I can no longer see those sanguine moonlit nights spent with Python no more than Frodo could see the Shire after being in possession of the One Ring for so long.

Whether this breaks the rules or not is irrelevant. The code is so bad it's of no use to anyone in it's current state. I'm more than a little surprised it works at all.

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