Sign in to follow this  
Followers 0
antisocialneedinghelp

So! Can autoit find lines from multiple textfiles and merge them into one?

11 posts in this topic

Quite simply...I want to find a way to write a script for merging information from textfiles via keywords.

M`k. I will have two text files.

the first text file, Will have a series of keywords. One keyword for each line in the file. It would be something simple, Such as a random series of numbers.

the second text file, Will be a cluster of information, With a few lines in that text file that have the keyword line from textfile 1 in it.

Now, I want those lines with the keyword from the textfile copied into a seperate list and saved. Not just the keyword, But the entire line that contains that keyword.

I have utilized textfiles on a few occasions in the past for uses such as storing information as a log for info that has gone through a script I have used. But nothing to this extent. I am pretty unfamiliar with textfile usage in autoit.

$file = FileWrite("temp.txt", $STX)
$file = FileOpen("temp.txt", 0)
$line = FileReadLine($file, 1)
Send($line, 1)
$file = FileWrite("Log.txt", $line)
$file = FileWrite("Log.txt", "$time")
$file = FileWriteLine("Log.txt", "")

I would love to expand my knowledge to using textfiles for writing more tools. But eh..Well. I suck at asking for help! ><!

If anyone here understands what I am trying to do <3 Then please step forward.

Share this post


Link to post
Share on other sites



Found this thread....

It seems to be a similar idea to what I am looking for...but I cant seem to properly apply it.

Share this post


Link to post
Share on other sites

Something like:

#include 

$string = "My line contains the word MKISH"
$string2 = "MKISH MKISH everywhere"
$string1 = "NO word"
FileWrite("example.txt", $string & @crlf & $string2 & @CRLF & $string1)
msgbox(64, "", "Example txt file created. I will search for 'MKISH'")

For $i = 1 to Int(_FileCountLines("example.txt"))
If StringInStr(FileReadLine("example.txt", $i), "MKISH") then FileWrite("MKISH.txt", FileReadLine("example.txt", $i) & @crlf)
Next

Change the word "MKISH" to whatever your search is...

1 person likes this

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Something like:

#include

$string = "My line contains the word MKISH"
$string2 = "MKISH MKISH everywhere"
$string1 = "NO word"
FileWrite("example.txt", $string & @crlf & $string2 & @CRLF & $string1)
msgbox(64, "", "Example txt file created. I will search for 'MKISH'")

For $i = 1 to Int(_FileCountLines("example.txt"))
If StringInStr(FileReadLine("example.txt", $i), "MKISH") then FileWrite("MKISH.txt", FileReadLine("example.txt", $i) & @crlf)
Next

Change the word "MKISH" to whatever your search is...

Well. the thing is, As I said in the first post, I am trying to find a way to have it not just look for "One word" But, A list of many many keywords within the file (In excess of 1,000) then copy that line to a text file.

Is it possible for me to set MKISH to something like. $FirstLine then after the line add something like delete the first line in the text file, And simply loop until it runs out of lines in the text file?

I suggest simply looping reading the first line, finding it, then deleteing the line it checked from the list of keywords since it seems like it would be easier for someone to know how to do that over going through a list.

Basicly....

#include

$string = "My line contains the word $Get Line 1 from keyword textfile blablabla"
$string2 = "MKISH MKISH everywhere"
$string1 = "NO word"
FileWrite("example.txt", $string & @crlf & $string2 & @CRLF & $string1)
msgbox(64, "", "Example txt file created. I will search for 'MKISH'")

For $i = 1 to Int(_FileCountLines("example.txt"))
If StringInStr(FileReadLine("example.txt", $i), "MKISH") then FileWrite("MKISH.txt", FileReadLine("example.txt", $i) & @crlf)
Delete line 1 from keyword textfile
Next

Unless someone knows how to search one line in the textfile, then the next, then the next, and so on.

Edit:

$value34 = InputBox("Variable Scan", "Search for...")
msgbox(64, "", "Example txt file created. I will search for " & $value34)

For $i = 1 to Int(_FileCountLines("example.txt"))
If StringInStr(FileReadLine("example.txt", $i), $value34) then FileWrite("Results.txt", FileReadLine("example.txt", $i) & @crlf)
Next

Script isnt bad....But, I would just like a way to replace the $value34 input box with a textfile of keywords, first line, second line, and so on. until the entire list is done.

Edited by antisocialneedinghelp

Share this post


Link to post
Share on other sites

Unless someone knows how to search one line in the textfile, then the next, then the next, and so on.

See the example code for this function - FileReadLine, this might help.

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

Do

$file = FileOpen("Keywords.txt", 0)
$line = FileReadLine($file, 1)

For $i = 1 to Int(_FileCountLines("ClusterOfData.txt"))
If StringInStr(FileReadLine("ClusterOfData.txt", $i), $line) then FileWrite("Results.txt", FileReadLine("ClusterOfData.txt", $i) & @crlf)

Next
_FileWriteToLine ("Keywords.txt", 1, "", 1)
FileClose($file)




$count =1

Until $count = 999999999999999999999999999999999999999999999999999999999999999999999999

There is my current progress....

Scans the textfile for the info...finds it, Deletes the keyword from the original list (AKA Line one) Then repeats the process almost endlessly.

Only problem now is...By scanning one at a time, It is taking Way Way too long....

Tried to duplicate line 7 and 4 with variables included such as $line5 = FileReadLine($file, 1)

and well, You get the idea.

It didnt seem to help with its speed at all...The textfiles are just large and need to search everything in one big clump if possible.

Share this post


Link to post
Share on other sites

Maybe it was a bad idea to just pull the first line from the textfile and then delete like this...Now it seems to just endlessly try to pull a non-existent line when it runs out of them never stopping.

Share this post


Link to post
Share on other sites

Maybe this will work for you. I tested it with a short keyword list, and a small "text cluster file". It's probably not perfect...

#include <File.au3>
#include <Misc.au3>

Opt("MustDeclareVars", 1)
Global $hDLL = DllOpen("user32.dll")
Global $keywordsFile = 'keywords.txt'
Global $textCluster = 'text.txt'
Global $iFile, $aKeyWords, $iNumOfLines, $hResultFile
Global $i, $ii, $iCount = 1, $sRet
$iFile = _FileReadToArray($keywordsFile, $aKeyWords)

If $iFile = 0 Then
MsgBox(48, 'ERROR', 'Error: ' & @error & @CRLF & 'Check help file for _FileReadToArray() function')
Exit
EndIf

$hResultFile = FileOpen('result.txt',1)

$iNumOfLines = _FileCountLines($textCluster)

For $i = 1 To $iNumOfLines
If _IsPressed('1b', $hDLL) Then ; Press the escape key to quit
ExitLoop
EndIf

If $iCount = $iNumOfLines Then
ExitLoop
EndIf

$sRet = FileReadLine($textCluster, $iCount)

For $ii = 1 To UBound($aKeyWords) -1
If StringInStr($sRet, $aKeyWords[$ii], 2) Then
FileWrite($hResultFile, $sRet & @CRLF)
EndIf
Next
$iCount = $iCount + 1
Next
FileClose($hResultFile)
1 person likes this

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Maybe this will work for you. I tested it with a short keyword list, and a small "text cluster file". It's probably not perfect...

#include <File.au3>
#include <Misc.au3>

Opt("MustDeclareVars", 1)
Global $hDLL = DllOpen("user32.dll")
Global $keywordsFile = 'keywords.txt'
Global $textCluster = 'text.txt'
Global $iFile, $aKeyWords, $iNumOfLines, $hResultFile
Global $i, $ii, $iCount = 1, $sRet
$iFile = _FileReadToArray($keywordsFile, $aKeyWords)

If $iFile = 0 Then
MsgBox(48, 'ERROR', 'Error: ' & @error & @CRLF & 'Check help file for _FileReadToArray() function')
Exit
EndIf

$hResultFile = FileOpen('result.txt',1)

$iNumOfLines = _FileCountLines($textCluster)

For $i = 1 To $iNumOfLines
If _IsPressed('1b', $hDLL) Then ; Press the escape key to quit
ExitLoop
EndIf

If $iCount = $iNumOfLines Then
ExitLoop
EndIf

$sRet = FileReadLine($textCluster, $iCount)

For $ii = 1 To UBound($aKeyWords) -1
If StringInStr($sRet, $aKeyWords[$ii], 2) Then
FileWrite($hResultFile, $sRet & @CRLF)
EndIf
Next
$iCount = $iCount + 1
Next
FileClose($hResultFile)

Works Very fast <3<3<3

I do have one question though.

I tried to rename "keywords.txt" and "text.txt" to Object1.txt and Object2.txt and it seemed to break the program...Any clue what causes this? Or how I could rename them without the code breaking?

Edit:

And yes, I renamed the textfiles to match the lines of code

Edited by antisocialneedinghelp

Share this post


Link to post
Share on other sites

And another example.

#include <File.au3>

; -------- Create test files ------------------
If FileExists("KeywordFile.txt") Then FileDelete("KeywordFile.txt")
FileWrite("KeywordFile.txt", "123" & @LF & "345" & @LF & "678" & @LF & "321" & @LF & "45" & @LF & "21" & @LF & "12" & @LF)

If FileExists("Object1.txt") Then FileDelete("Object1.txt")
FileWrite("Object1.txt", "line 1 " & @LF & "line 2" & @LF & "line 3 F1" & @LF & "line 4" & @LF & "line 5" & @LF & "line 6")

If FileExists("Object2.txt") Then FileDelete("Object2.txt")
FileWrite("Object2.txt", "line 1 45 Object2" & @LF & "line 2" & @LF & "line 3 F2" & @LF & "line 4 Object2.txt 12" & @LF & "line 5")

If FileExists("Object3.txt") Then FileDelete("Object3.txt")
FileWrite("Object3.txt", "line 1 " & @LF & "line 2" & @LF & "line 3 F3" & @LF & "line 4" & @LF & "line 5 21 Object3.txt")
; --------> End of Create test files ------------------

Local $sKeyWords = StringRegExpReplace(StringStripWS(FileRead("KeywordFile.txt"), 2), "(R)", "|")
;ConsoleWrite($sKeyWords & @LF)

;Local $aArrayFiles = _FileListToArray($sPath, "Object*.txt",1)
Local $aArrayFiles[4] = [3, "Object1.txt", "Object2.txt", "Object3.txt"]
Local $sFileContents, $sLines = "", $a

For $i = 1 To UBound($aArrayFiles) - 1
    $sFileContents = FileRead($aArrayFiles[$i])
    If StringRegExp($sFileContents, $sKeyWords) Then
        $a = StringRegExp($sFileContents, "(.*(?:" & $sKeyWords & ").*)", 3) & @CRLF
        For $j = 0 To UBound($a) - 1
            $sLines &= $a[$j] & @LF
        Next
    EndIf
Next

ConsoleWrite($sLines & @LF)
;FileWrite("NewFile.txt",$sLines"

;Clean up files
FileDelete("KeywordFile.txt")
For $i = 1 To UBound($aArrayFiles) - 1
    FileDelete($aArrayFiles[$i])
Next

Share this post


Link to post
Share on other sites

Works Very fast <3<3<3

I do have one question though.

I tried to rename "keywords.txt" and "text.txt" to Object1.txt and Object2.txt and it seemed to break the program...Any clue what causes this? Or how I could rename them without the code breaking?

Edit:

And yes, I renamed the textfiles to match the lines of code

If you ran my script as it is and you just renamed your text files to match the the filenames in the script, and it worked for you then the lines you edited in the script are most likely the problem. I noticed you don't have quotes surrounding your new names. Did you forget them in the script?

However; I noticed that my script terminated before the last line in the "text cluster file" was checked. Should be fixed in the script below.

#include <File.au3>
#include <Misc.au3>

Opt("MustDeclareVars", 1)

Global $hDLL = DllOpen("user32.dll")
Global $keywordsFile = 'keywords.txt'
Global $textCluster = 'text.txt'
Global $iFile, $aKeyWords, $iNumOfLines, $hResultFile
Global $i, $ii, $iCount = 1, $sRet

;Check for keywords file
If Not FileExists($keywordsFile) Then
MsgBox(48, 'ERROR', 'Keywords file not found.' & @CRLF& 'Program Terminated.')
Exit
EndIf

;Check for text cluster file
If Not FileExists($textCluster) Then
MsgBox(48, 'ERROR', 'Text Cluster file not found.' & @CRLF& 'Program Terminated.')
Exit
EndIf

$iFile = _FileReadToArray($keywordsFile, $aKeyWords)

If $iFile = 0 Then
MsgBox(48, 'ERROR', 'Error: ' & @error & @CRLF & 'Check Autoit help file for _FileReadToArray() function')
Exit
EndIf

$hResultFile = FileOpen('result.txt',1)

$iNumOfLines = _FileCountLines($textCluster)

For $i = 1 To $iNumOfLines
If _IsPressed('1b', $hDLL) Then ; Press the escape key to quit
ExitLoop
EndIf

$sRet = FileReadLine($textCluster, $iCount)

For $ii = 1 To UBound($aKeyWords, 1) -1
If StringInStr($sRet, $aKeyWords[$ii], 2, -1) Then
FileWrite($hResultFile, $sRet & @CRLF)
EndIf
Next

If $iCount = $iNumOfLines Then
ExitLoop
EndIf

$iCount = $iCount + 1
Next

FileClose($hResultFile)
DllClose($hDLL)
Exit
1 person likes this

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