face Posted April 30, 2014 Share Posted April 30, 2014 (edited) how can i copy and save as a list (in .txt format) all text lines from text files located in folders and sub folders eg: valuable house valuable car valuable boat it will copy every line that starts with the word "valuable" and saves it in linelist.txt Edited April 30, 2014 by face Link to comment Share on other sites More sharing options...
mikell Posted April 30, 2014 Share Posted April 30, 2014 What did you try so far ? Link to comment Share on other sites More sharing options...
face Posted April 30, 2014 Author Share Posted April 30, 2014 didn't program or try anything yet, hopefully someone will post a sample Link to comment Share on other sites More sharing options...
somdcomputerguy Posted April 30, 2014 Share Posted April 30, 2014 (edited) http://www.autoitscript.com/autoit3/docs/functions/StringLeft.htm Edited April 30, 2014 by somdcomputerguy - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change. Link to comment Share on other sites More sharing options...
face Posted April 30, 2014 Author Share Posted April 30, 2014 heres some code i came up with but doesn't seem to work #include <File.au3> #include <Array.au3> #include <MsgBoxConstants.au3> $file_output = @DesktopDir & "\output.txt" Local $sString = StringLeft("valuable", 100) Local $mypath = @DesktopDir Local $aFiles = _FileListToArray($mypath, "*.txt", 1, 1) Local $array[1] _FileReadToArray($afiles, $array) $hOutput = FileOpen($file_output, 1000) If @error Then MsgBox($MB_SYSTEMMODAL, "Error", "No files found") Exit Else MsgBox($MB_SYSTEMMODAL, "Found", $aFiles[0] & " files") EndIf FileWrite("saved/words.txt", _ArrayToString($sString, @CRLF)) it has to work with Local $sString = StringLeft("valuable", 100) it needs to copy every line that starts with the word "valuable" and save it to file output.txt Link to comment Share on other sites More sharing options...
mikell Posted April 30, 2014 Share Posted April 30, 2014 Try this #include <File.au3> #include <Array.au3> $file_output = @DesktopDir & "\output.txt" Local $mypath = @DesktopDir, $lines = "" Local $aFiles = _FileListToArrayRec($mypath, "*.txt", 1, 1, 0, 2) For $i = 1 to $aFiles[0] $content = FileRead($aFiles[$i]) $res = StringRegExp($content, '(?m)^valuable\s*(.*)\R?', 3) If IsArray($res) Then $lines &= $aFiles[$i] & @CRLF $lines &= _ArrayToString($res, @CRLF) & @CRLF & @CRLF EndIf Next FileWrite($file_output, $lines) Link to comment Share on other sites More sharing options...
face Posted April 30, 2014 Author Share Posted April 30, 2014 (edited) Try this #include <File.au3> #include <Array.au3> $file_output = @DesktopDir & "\output.txt" Local $mypath = @DesktopDir, $lines = "" Local $aFiles = _FileListToArrayRec($mypath, "*.txt", 1, 1, 0, 2) For $i = 1 to $aFiles[0] $content = FileRead($aFiles[$i]) $res = StringRegExp($content, '(?m)^valuable\s*(.*)\R?', 3) If IsArray($res) Then $lines &= $aFiles[$i] & @CRLF $lines &= _ArrayToString($res, @CRLF) & @CRLF & @CRLF EndIf Next FileWrite($file_output, $lines) it copies entire paragraphs afer the word valuable and doesnt save/include the word valuable in the output file. it has to copy entire rows that start with "valuable" and also include the word valuable in the output file in the saved phrase Edited April 30, 2014 by face Link to comment Share on other sites More sharing options...
mikell Posted April 30, 2014 Share Posted April 30, 2014 Change the line '$res = ...' to this new one $res = StringRegExp($content, '(?m)^(valuable\N*)\R?', 3) Link to comment Share on other sites More sharing options...
somdcomputerguy Posted April 30, 2014 Share Posted April 30, 2014 I mis-read the first post. I thought that you wanted to write each line that started with "valuable" without "valuable". So that "valuable house" would become "house". For that, you would need to use the StringLeft() function, or the StringTrimLeft() funtion, preceded by the StringInStr() function, which I failed to mention. To find each line that starts with "Valuable", all you need is the StringInStr() function, and the StringLeft() function to verify that the first word is "Valuable". Now this can be done alot cleaner with the StringRegExp() function. Due to another post here, and a question on another forum, I have decided to spend the next hour or so making myself more teached about it. Tomorrow I will have forgotten everything I learn today, but I will remember to start over.. - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change. Link to comment Share on other sites More sharing options...
face Posted May 1, 2014 Author Share Posted May 1, 2014 Change the line '$res = ...' to this new one $res = StringRegExp($content, '(?m)^(valuable\N*)\R?', 3) it works very well but it saves the link to the file, can you remove the file adress eg: C:UsersuserDesktoptexttemp.txt valuable 1 valuable two Link to comment Share on other sites More sharing options...
somdcomputerguy Posted May 1, 2014 Share Posted May 1, 2014 (edited) Local $inputfile = FileOpen("face_input.txt", 0) Local $outputfile = FileOpen("face_output.txt", 1) If $inputfile = -1 Then MsgBox(0, "Error", "Unable to open input file.") Exit EndIf If $outputfile = -1 Then MsgBox(0, "Error", "Unable to open output file.") Exit EndIf ; Read in lines of $inputfile until the EOF is reached While 1 Local $line = FileReadLine($inputfile) If @error = -1 Then ExitLoop If StringInStr($line, "valuable") AND StringLeft($line, 8) = "valuable" Then ;ConsoleWrite("This line: " & $line & " would be written." & @LF) FileWriteLine($outputfile, $line & @CRLF) EndIf WEnd FileClose($inputfile) FileClose($outputfile)face_input.txt containshouse not a very valuable car valuable boatadd: the tag gods are on a break again.. Edited May 1, 2014 by somdcomputerguy - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change. Link to comment Share on other sites More sharing options...
mikell Posted May 1, 2014 Share Posted May 1, 2014 Tomorrow I will have forgotten everything I learn today This would be a pity, please don't ! face, I thought that getting the address of the files from which the informations came was a good idea... The code below will return a simple list #include <File.au3> #include <Array.au3> $file_output = @DesktopDir & "\output.txt" Local $mypath = @DesktopDir, $lines = "" Local $aFiles = _FileListToArrayRec($mypath, "*.txt", 1, 1, 0, 2) For $i = 1 to $aFiles[0] Local $content = FileRead($aFiles[$i]) Local $res = StringRegExp($content, '(?m)^(valuable\N*)\R?', 3) If IsArray($res) Then $lines &= _ArrayToString($res, @CRLF) &@CRLF EndIf Next FileWrite($file_output, $lines) 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