Jump to content

copy text file text lines starting with


face
 Share

Recommended Posts

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 by face
Link to comment
Share on other sites

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

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

 

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 by face
Link to comment
Share on other sites

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

 

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

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 contains

house
not a very valuable car
valuable boat
add: the tag gods are on a break again.. Edited 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

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...