Sign in to follow this  
Followers 0
James

Read certain text from file

7 posts in this topic

Hi all,

I am looking for a way to read a certain string of text from a file. I have tried to do it like so:

$_File = FileOpen("mylog.log", 1)
$Read = FileRead($_File)
If StringInStr($Read, "Log time:") Then MsgBox(0, "", "")

Probably the easiest of them all but I have no idea. The line could be anywhere in the file.

Thanks,

James

Share this post


Link to post
Share on other sites



I like to do it like this but there are other ways. This returns what line it is on. Doing it line by line reduces the character amount in the buffer for stringinstr to check so I find it much better, but that is just a personal preference.

#include <File.au3>
$File = @ScriptDir & '\SomeFile.txt'
For $i = 1 To _FileCountLines($File)
    If StringInStr(FileReadLine($File,$i),'Some String of Text') Then MsgBox(0,'Here','Text is on line: ' & $i)
Next

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here is the code I am using:

$_DriveSet = DriveGetDrive("removable")
For $b = 1 To $_DriveSet[0]
                If $_DriveSet[$b] <> "a:" Then
                    If FileExists($_DriveSet[$b] & "\autorun.inf") Then
                ; MsgBox(0, "AutoTray", "Autorun.inf was found!")
                ; Start watching for autorun.inf files
                        Global $Name = $_DriveSet[$b] & '\autorun.inf'
                        $_File = FileOpen($Name, 0)
                        For $c = 1 To _FileCountLines($_File)
                            If StringInStr(FileReadLine($_File, $c), "[autorun]") Then MsgBox(0, "AutoTray", "Autorun header was found!")
                        Next
                    Else
                ; Message the user if autorun.inf doesn't exist
                        $_Cnt = MsgBox(0, "AutoTray", "Autorun.inf was not found to be in any removable devices.")
                    EndIf
                EndIf
            Next

However it doesn't find anything...

Edited by JamesB

Share this post


Link to post
Share on other sites

Here is the code I am using:

$_DriveSet = DriveGetDrive("removable")
For $b = 1 To $_DriveSet[0]
                If $_DriveSet[$b] <> "a:" Then
                    If FileExists($_DriveSet[$b] & "\autorun.inf") Then
            ; MsgBox(0, "AutoTray", "Autorun.inf was found!")
            ; Start watching for autorun.inf files
                        Global $Name = $_DriveSet[$b] & '\autorun.inf'
                        $_File = FileOpen($Name, 0)
                        For $c = 1 To _FileCountLines($_File)
                            If StringInStr(FileReadLine($_File, $c), "[autorun]") Then MsgBox(0, "AutoTray", "Autorun header was found!")
                        Next
                    Else
            ; Message the user if autorun.inf doesn't exist
                        $_Cnt = MsgBox(0, "AutoTray", "Autorun.inf was not found to be in any removable devices.")
                    EndIf
                EndIf
            Next

However it doesn't find anything...

Try using _FileCountLines with the actual file, not handle of the file. So $Name

Share this post


Link to post
Share on other sites

I cant, because it's a loop to find the drive letter.

What do you mean you can't ,

I made a file added, in [autorun] and some other junk then used this and it works just fine

Global $Name = @DesktopDir & '\some.txt'
                        $_File = FileOpen($Name, 0)
                        For $c = 1 To _FileCountLines($Name)
                            If StringInStr(FileReadLine($Name, $c), "[autorun]") Then MsgBox(0, "AutoTray", "Autorun header was found!")
                        Next

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