Jump to content
Sign in to follow this  
TuMiM

Search within file

Recommended Posts

TuMiM

Is there a way to search within the contents of a file for different things and then write them to a file? for example if the file contains test: mytest then write it to a file. If it contains test: mytest2 write it to a file. But the file can contain multiple of these entries so it would have to search for all of them. Is that possible?

Share this post


Link to post
Share on other sites
ChrisL

Do you mean like this?

$File = FileOpen ("MyTextfile.txt",0)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    
    $line = FileReadLine ( $File)
    if @error = -1 then ExitLoop
    If Stringinstr ($line, "MyString") then FileWriteLine ("MyNewfile.txt", $Line)
    WEnd
    FileClose ($File)
Edited by ChrisL

Share this post


Link to post
Share on other sites
cdkid

$var = FileRead("myfile.txt", FileGetSize("myfile.txt"))
$var = StringSplit($var, @CRLF, 1)
for $i = 0 to $var[0]
if StringInStr($var[$i], "test: mytest") then
filewriteline("mynewfile.txt",$var[$i])
EndIf
Next

--like that?

~cdkid


AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!

Share this post


Link to post
Share on other sites
TuMiM

ok, let's say this file is not really meant to be open as a text file but if you open it with notepad you can do a find with notepad and easily find these entries. Should the fileread command work? It is not working on these files but works fine on a regular txt file. what does fileread use to open the file?

Share this post


Link to post
Share on other sites
cdkid

can you attatch the file you want to read from? Because i dont really understand waht you mean.

~cdkid


AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!

Share this post


Link to post
Share on other sites
randallc

Hi,

if you want the whole line, yo can use DOS; [in Search String, separate multiple searches by spaces; check syntax for Findstr]

1.

Dim $DosFINDSTRReturn,$Display

Opt("RunErrorsFatal", 0)

$SearchString='LANG'

$DefSwitches='/R/I /c:"'&$SearchString&'"' ; Change only if you know FINDSTR switches well

$FileIndiv = @ScriptDir&"\849064.txt"

$Answer=@ScriptDir&"\Answer.txt"

FileDelete($Answer)

FINDSTRAU3($DefSwitches,$FileIndiv,$Answer)

RunWait("notepad.exe " & $Answer ,@ScriptDir)

;--------------- FINDSTRAU3 function; Randallc randallc@ozemail.com.au ---------------

Func FINDSTRAU3($Switches, $File,$AnswerFile)

If StringInStr($File, " ") Then $File = '"' & $File & '"'

If StringInStr($AnswerFile, " ") Then $AnswerFile = '"' & $AnswerFile & '"'

$Command ='FINDSTR '& $Switches &' '& $File &'>'&$AnswerFile

$DosFINDSTRReturn=RunWait( @ComSpec & " /c " & $Command, @SystemDir, @SW_HIDE)

EndFunc ;==>FINDSTRAU3

OR FileRead works OK;

2.

$sTXTLOGFile = @ScriptDir&"\849064.txt"

$sTXTLOGFile2 = @ScriptDir&"\sTXTLOGFile2.txt"

$var = FileRead($sTXTLOGFile, FileGetSize($sTXTLOGFile))

FileWriteLine ($sTXTLOGFile2, $var)

RunWait("notepad.exe " & $sTXTLOGFile2 ,@ScriptDir)

if StringInStr($var,"LANG") then $AnswerString="LANG"

if StringInStr($var,"LANG2") then $AnswerString="LANG2"

MsgBox(0,"","$AnswerString="&$AnswerString)

Best, Randall Edited by randallc

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.