James Posted February 2, 2008 Share Posted February 2, 2008 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 Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Thatsgreat2345 Posted February 2, 2008 Share Posted February 2, 2008 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 Link to comment Share on other sites More sharing options...
James Posted February 2, 2008 Author Share Posted February 2, 2008 (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 February 2, 2008 by JamesB Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Thatsgreat2345 Posted February 2, 2008 Share Posted February 2, 2008 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 Link to comment Share on other sites More sharing options...
James Posted February 2, 2008 Author Share Posted February 2, 2008 (edited) I cant, because it's a loop to find the drive letter. Edit: I tried the actual directory link and it didn't work. Edited February 2, 2008 by JamesB Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Thatsgreat2345 Posted February 2, 2008 Share Posted February 2, 2008 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 Link to comment Share on other sites More sharing options...
James Posted February 2, 2008 Author Share Posted February 2, 2008 I must of done something wrong, because it works now. Very strange. Thanks! Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now